zeph-tools 0.18.2

Tool executor trait with shell, web scrape, and composite executors for Zeph
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
759
760
761
762
763
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! 12-category tool invocation error taxonomy (arXiv:2601.16280).
//!
//! Provides fine-grained error classification beyond the binary `ErrorKind`
//! (Transient/Permanent), enabling category-specific recovery strategies,
//! structured LLM feedback, and quality-attributable reputation scoring.

use crate::executor::ErrorKind;

/// Invocation phase in which a tool failure occurred, per arXiv:2601.16280.
///
/// Maps Zeph's `ToolErrorCategory` variants to the 4-phase diagnostic framework:
/// Setup → `ParamHandling` → Execution → `ResultInterpretation`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolInvocationPhase {
    /// Tool lookup/registration phase: was the tool name valid?
    Setup,
    /// Parameter validation phase: were the provided arguments well-formed?
    ParamHandling,
    /// Runtime execution phase: did the tool run successfully?
    Execution,
    /// Output parsing/interpretation phase: was the result usable?
    /// Reserved for future use — no current `ToolErrorCategory` maps here.
    ResultInterpretation,
}

impl ToolInvocationPhase {
    /// Human-readable label for audit logs.
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Self::Setup => "setup",
            Self::ParamHandling => "param_handling",
            Self::Execution => "execution",
            Self::ResultInterpretation => "result_interpretation",
        }
    }
}

/// High-level error domain for recovery strategy dispatch.
///
/// Groups the 11 `ToolErrorCategory` variants into 4 domains that map to distinct
/// recovery strategies in the agent loop. Does NOT replace `ToolErrorCategory` — it
/// is a companion abstraction for coarse dispatch.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ErrorDomain {
    /// The agent selected the wrong tool or misunderstood the task.
    /// Recovery: re-plan, pick a different tool or approach.
    /// Categories: `ToolNotFound`
    Planning,

    /// The agent's output (parameters, types) was malformed.
    /// Recovery: reformat parameters using tool schema, retry once.
    /// Categories: `InvalidParameters`, `TypeMismatch`
    Reflection,

    /// External action failed due to policy or resource constraints.
    /// Recovery: inform user, suggest alternative, or skip.
    /// Categories: `PolicyBlocked`, `ConfirmationRequired`, `PermanentFailure`, `Cancelled`
    Action,

    /// Transient infrastructure failure.
    /// Recovery: automatic retry with backoff.
    /// Categories: `RateLimited`, `ServerError`, `NetworkError`, `Timeout`
    System,
}

impl ErrorDomain {
    /// Whether errors in this domain should trigger automatic retry.
    #[must_use]
    pub fn is_auto_retryable(self) -> bool {
        matches!(self, Self::System)
    }

    /// Whether the LLM should be asked to fix its output.
    #[must_use]
    pub fn needs_llm_correction(self) -> bool {
        matches!(self, Self::Reflection | Self::Planning)
    }

    /// Human-readable label for audit logs.
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Self::Planning => "planning",
            Self::Reflection => "reflection",
            Self::Action => "action",
            Self::System => "system",
        }
    }
}

/// Fine-grained 12-category classification of tool invocation errors.
///
/// Each category determines retry eligibility, LLM parameter reformat path,
/// quality attribution for reputation scoring, and structured feedback content.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
pub enum ToolErrorCategory {
    // ── Initialization failures ──────────────────────────────────────────
    /// Tool name not found in the registry (LLM requested a non-existent tool).
    ToolNotFound,

    // ── Parameter failures ───────────────────────────────────────────────
    /// LLM provided invalid or missing parameters for the tool.
    InvalidParameters,
    /// Parameter type mismatch (e.g., string where integer expected).
    TypeMismatch,

    // ── Permission / policy failures ─────────────────────────────────────
    /// Blocked by security policy (blocklist, sandbox, trust gate).
    PolicyBlocked,
    /// Requires user confirmation before execution.
    ConfirmationRequired,

    // ── Execution failures (permanent) ───────────────────────────────────
    /// HTTP 403/404 or equivalent permanent resource rejection.
    PermanentFailure,
    /// Operation cancelled by the user.
    Cancelled,

    // ── Execution failures (transient) ───────────────────────────────────
    /// HTTP 429 (rate limit) or resource exhaustion.
    RateLimited,
    /// HTTP 5xx or equivalent server-side error.
    ServerError,
    /// Network connectivity failure (DNS, connection refused, reset).
    NetworkError,
    /// Operation timed out.
    Timeout,
}

impl ToolErrorCategory {
    /// Whether this error category is eligible for automatic retry with backoff.
    #[must_use]
    pub fn is_retryable(self) -> bool {
        matches!(
            self,
            Self::RateLimited | Self::ServerError | Self::NetworkError | Self::Timeout
        )
    }

    /// Whether the LLM should be asked to reformat parameters and retry.
    ///
    /// Only `InvalidParameters` and `TypeMismatch` trigger the reformat path.
    /// A single reformat attempt is allowed; if it fails, the error is final.
    #[must_use]
    pub fn needs_parameter_reformat(self) -> bool {
        matches!(self, Self::InvalidParameters | Self::TypeMismatch)
    }

    /// Whether this error is attributable to LLM output quality.
    ///
    /// Quality failures affect reputation scoring in triage routing and are the
    /// only category for which `attempt_self_reflection` should be triggered.
    /// Infrastructure errors (network, timeout, server, rate limit) are NOT
    /// the model's fault and must never trigger self-reflection.
    #[must_use]
    pub fn is_quality_failure(self) -> bool {
        matches!(
            self,
            Self::InvalidParameters | Self::TypeMismatch | Self::ToolNotFound
        )
    }

    /// Map to the high-level error domain for recovery dispatch.
    ///
    /// Use the returned `ErrorDomain` to select a recovery strategy in the agent loop
    /// instead of checking multiple predicate methods individually.
    #[must_use]
    pub fn domain(self) -> ErrorDomain {
        match self {
            Self::ToolNotFound => ErrorDomain::Planning,
            Self::InvalidParameters | Self::TypeMismatch => ErrorDomain::Reflection,
            Self::PolicyBlocked
            | Self::ConfirmationRequired
            | Self::PermanentFailure
            | Self::Cancelled => ErrorDomain::Action,
            Self::RateLimited | Self::ServerError | Self::NetworkError | Self::Timeout => {
                ErrorDomain::System
            }
        }
    }

    /// Coarse classification for backward compatibility with existing `ErrorKind`.
    #[must_use]
    pub fn error_kind(self) -> ErrorKind {
        if self.is_retryable() {
            ErrorKind::Transient
        } else {
            ErrorKind::Permanent
        }
    }

    /// Map to the diagnostic invocation phase per arXiv:2601.16280.
    #[must_use]
    pub fn phase(self) -> ToolInvocationPhase {
        match self {
            Self::ToolNotFound => ToolInvocationPhase::Setup,
            Self::InvalidParameters | Self::TypeMismatch => ToolInvocationPhase::ParamHandling,
            Self::PolicyBlocked
            | Self::ConfirmationRequired
            | Self::PermanentFailure
            | Self::Cancelled
            | Self::RateLimited
            | Self::ServerError
            | Self::NetworkError
            | Self::Timeout => ToolInvocationPhase::Execution,
        }
    }

    /// Human-readable label for audit logs, TUI status indicators, and structured feedback.
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Self::ToolNotFound => "tool_not_found",
            Self::InvalidParameters => "invalid_parameters",
            Self::TypeMismatch => "type_mismatch",
            Self::PolicyBlocked => "policy_blocked",
            Self::ConfirmationRequired => "confirmation_required",
            Self::PermanentFailure => "permanent_failure",
            Self::Cancelled => "cancelled",
            Self::RateLimited => "rate_limited",
            Self::ServerError => "server_error",
            Self::NetworkError => "network_error",
            Self::Timeout => "timeout",
        }
    }

    /// Recovery suggestion for the LLM based on error category.
    #[must_use]
    pub fn suggestion(self) -> &'static str {
        match self {
            Self::ToolNotFound => {
                "Check the tool name. Use tool_definitions to see available tools."
            }
            Self::InvalidParameters => "Review the tool schema and provide correct parameters.",
            Self::TypeMismatch => "Check parameter types against the tool schema.",
            Self::PolicyBlocked => {
                "This operation is blocked by security policy. Try an alternative approach."
            }
            Self::ConfirmationRequired => "This operation requires user confirmation.",
            Self::PermanentFailure => {
                "This resource is not available. Try an alternative approach."
            }
            Self::Cancelled => "Operation was cancelled by the user.",
            Self::RateLimited => "Rate limit exceeded. The system will retry if possible.",
            Self::ServerError => "Server error. The system will retry if possible.",
            Self::NetworkError => "Network error. The system will retry if possible.",
            Self::Timeout => "Operation timed out. The system will retry if possible.",
        }
    }
}

/// Structured error feedback injected as `tool_result` content for classified errors.
///
/// Provides the LLM with actionable information about what went wrong and what to
/// do next, replacing the opaque `[error] ...` string format.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ToolErrorFeedback {
    pub category: ToolErrorCategory,
    pub message: String,
    pub retryable: bool,
}

impl ToolErrorFeedback {
    /// Format as a structured string for injection into `tool_result` content.
    #[must_use]
    pub fn format_for_llm(&self) -> String {
        format!(
            "[tool_error]\ncategory: {}\nerror: {}\nsuggestion: {}\nretryable: {}",
            self.category.label(),
            self.message,
            self.category.suggestion(),
            self.retryable,
        )
    }
}

/// Classify an HTTP status code into a `ToolErrorCategory`.
#[must_use]
pub fn classify_http_status(status: u16) -> ToolErrorCategory {
    match status {
        400 | 422 => ToolErrorCategory::InvalidParameters,
        401 | 403 => ToolErrorCategory::PolicyBlocked,
        429 => ToolErrorCategory::RateLimited,
        500..=599 => ToolErrorCategory::ServerError,
        // 404, 410, and all other non-success codes: permanent failure.
        _ => ToolErrorCategory::PermanentFailure,
    }
}

/// Classify an `io::Error` into a `ToolErrorCategory`.
///
/// # Note on `io::ErrorKind::NotFound`
///
/// `NotFound` from an `Execution` error means a file or binary was not found at the
/// OS level (e.g., `bash: command not found`). This is NOT the same as "tool not found
/// in registry" (`ToolNotFound`). We map it to `PermanentFailure` to avoid incorrectly
/// penalizing the model for OS-level path issues.
#[must_use]
pub fn classify_io_error(err: &std::io::Error) -> ToolErrorCategory {
    match err.kind() {
        std::io::ErrorKind::TimedOut => ToolErrorCategory::Timeout,
        std::io::ErrorKind::ConnectionRefused
        | std::io::ErrorKind::ConnectionReset
        | std::io::ErrorKind::ConnectionAborted
        | std::io::ErrorKind::BrokenPipe => ToolErrorCategory::NetworkError,
        // WouldBlock / Interrupted are async runtime signals, not true network failures,
        // but they are transient and retryable — map to ServerError as the generic
        // retryable catch-all rather than NetworkError to avoid misleading audit labels.
        std::io::ErrorKind::WouldBlock | std::io::ErrorKind::Interrupted => {
            ToolErrorCategory::ServerError
        }
        std::io::ErrorKind::PermissionDenied => ToolErrorCategory::PolicyBlocked,
        // OS-level file/binary not found is a permanent execution failure, not a registry miss.
        // ToolNotFound is reserved for registry misses (LLM requested an unknown tool name).
        _ => ToolErrorCategory::PermanentFailure,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn retryable_categories() {
        assert!(ToolErrorCategory::RateLimited.is_retryable());
        assert!(ToolErrorCategory::ServerError.is_retryable());
        assert!(ToolErrorCategory::NetworkError.is_retryable());
        assert!(ToolErrorCategory::Timeout.is_retryable());

        assert!(!ToolErrorCategory::InvalidParameters.is_retryable());
        assert!(!ToolErrorCategory::TypeMismatch.is_retryable());
        assert!(!ToolErrorCategory::ToolNotFound.is_retryable());
        assert!(!ToolErrorCategory::PolicyBlocked.is_retryable());
        assert!(!ToolErrorCategory::PermanentFailure.is_retryable());
        assert!(!ToolErrorCategory::Cancelled.is_retryable());
        assert!(!ToolErrorCategory::ConfirmationRequired.is_retryable());
    }

    #[test]
    fn quality_failure_categories() {
        assert!(ToolErrorCategory::InvalidParameters.is_quality_failure());
        assert!(ToolErrorCategory::TypeMismatch.is_quality_failure());
        assert!(ToolErrorCategory::ToolNotFound.is_quality_failure());

        // Infrastructure errors must NOT be quality failures — they must not trigger
        // self-reflection, as they are not attributable to LLM output quality.
        assert!(!ToolErrorCategory::NetworkError.is_quality_failure());
        assert!(!ToolErrorCategory::ServerError.is_quality_failure());
        assert!(!ToolErrorCategory::RateLimited.is_quality_failure());
        assert!(!ToolErrorCategory::Timeout.is_quality_failure());
        assert!(!ToolErrorCategory::PolicyBlocked.is_quality_failure());
        assert!(!ToolErrorCategory::PermanentFailure.is_quality_failure());
        assert!(!ToolErrorCategory::Cancelled.is_quality_failure());
    }

    #[test]
    fn needs_parameter_reformat() {
        assert!(ToolErrorCategory::InvalidParameters.needs_parameter_reformat());
        assert!(ToolErrorCategory::TypeMismatch.needs_parameter_reformat());
        assert!(!ToolErrorCategory::NetworkError.needs_parameter_reformat());
        assert!(!ToolErrorCategory::ToolNotFound.needs_parameter_reformat());
    }

    #[test]
    fn error_kind_backward_compat() {
        // Retryable categories → Transient
        assert_eq!(
            ToolErrorCategory::NetworkError.error_kind(),
            ErrorKind::Transient
        );
        assert_eq!(
            ToolErrorCategory::Timeout.error_kind(),
            ErrorKind::Transient
        );
        // Non-retryable → Permanent
        assert_eq!(
            ToolErrorCategory::InvalidParameters.error_kind(),
            ErrorKind::Permanent
        );
        assert_eq!(
            ToolErrorCategory::PolicyBlocked.error_kind(),
            ErrorKind::Permanent
        );
    }

    #[test]
    fn classify_http_status_codes() {
        assert_eq!(classify_http_status(403), ToolErrorCategory::PolicyBlocked);
        assert_eq!(
            classify_http_status(404),
            ToolErrorCategory::PermanentFailure
        );
        assert_eq!(
            classify_http_status(422),
            ToolErrorCategory::InvalidParameters
        );
        assert_eq!(classify_http_status(429), ToolErrorCategory::RateLimited);
        assert_eq!(classify_http_status(500), ToolErrorCategory::ServerError);
        assert_eq!(classify_http_status(503), ToolErrorCategory::ServerError);
        assert_eq!(
            classify_http_status(200),
            ToolErrorCategory::PermanentFailure
        );
    }

    #[test]
    fn classify_io_not_found_is_permanent_not_tool_not_found() {
        // B2 fix: OS-level NotFound must NOT map to ToolNotFound.
        // ToolNotFound is reserved for registry misses (LLM requested unknown tool name).
        let err = std::io::Error::new(std::io::ErrorKind::NotFound, "No such file or directory");
        assert_eq!(classify_io_error(&err), ToolErrorCategory::PermanentFailure);
    }

    #[test]
    fn classify_io_connection_errors() {
        let refused =
            std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "connection refused");
        assert_eq!(classify_io_error(&refused), ToolErrorCategory::NetworkError);

        let reset = std::io::Error::new(std::io::ErrorKind::ConnectionReset, "reset");
        assert_eq!(classify_io_error(&reset), ToolErrorCategory::NetworkError);

        let timed_out = std::io::Error::new(std::io::ErrorKind::TimedOut, "timed out");
        assert_eq!(classify_io_error(&timed_out), ToolErrorCategory::Timeout);
    }

    #[test]
    fn tool_error_feedback_format() {
        let fb = ToolErrorFeedback {
            category: ToolErrorCategory::InvalidParameters,
            message: "missing required field: url".to_owned(),
            retryable: false,
        };
        let s = fb.format_for_llm();
        assert!(s.contains("[tool_error]"));
        assert!(s.contains("invalid_parameters"));
        assert!(s.contains("missing required field: url"));
        assert!(s.contains("retryable: false"));
    }

    #[test]
    fn all_categories_have_labels() {
        let categories = [
            ToolErrorCategory::ToolNotFound,
            ToolErrorCategory::InvalidParameters,
            ToolErrorCategory::TypeMismatch,
            ToolErrorCategory::PolicyBlocked,
            ToolErrorCategory::ConfirmationRequired,
            ToolErrorCategory::PermanentFailure,
            ToolErrorCategory::Cancelled,
            ToolErrorCategory::RateLimited,
            ToolErrorCategory::ServerError,
            ToolErrorCategory::NetworkError,
            ToolErrorCategory::Timeout,
        ];
        for cat in categories {
            assert!(!cat.label().is_empty(), "category {cat:?} has empty label");
            assert!(
                !cat.suggestion().is_empty(),
                "category {cat:?} has empty suggestion"
            );
        }
    }

    // ── classify_http_status: full coverage per taxonomy spec ────────────────

    #[test]
    fn classify_http_400_is_invalid_parameters() {
        assert_eq!(
            classify_http_status(400),
            ToolErrorCategory::InvalidParameters
        );
    }

    #[test]
    fn classify_http_401_is_policy_blocked() {
        assert_eq!(classify_http_status(401), ToolErrorCategory::PolicyBlocked);
    }

    #[test]
    fn classify_http_502_is_server_error() {
        assert_eq!(classify_http_status(502), ToolErrorCategory::ServerError);
    }

    // ── ToolErrorFeedback: category-specific content ──────────────────────────

    #[test]
    fn feedback_permanent_failure_not_retryable() {
        let fb = ToolErrorFeedback {
            category: ToolErrorCategory::PermanentFailure,
            message: "resource does not exist".to_owned(),
            retryable: false,
        };
        let s = fb.format_for_llm();
        assert!(s.contains("permanent_failure"));
        assert!(s.contains("resource does not exist"));
        assert!(s.contains("retryable: false"));
        // Suggestion must not mention auto-retry for a permanent error.
        let suggestion = ToolErrorCategory::PermanentFailure.suggestion();
        assert!(!suggestion.contains("retry automatically"), "{suggestion}");
    }

    #[test]
    fn feedback_rate_limited_is_retryable_and_mentions_retry() {
        let fb = ToolErrorFeedback {
            category: ToolErrorCategory::RateLimited,
            message: "too many requests".to_owned(),
            retryable: true,
        };
        let s = fb.format_for_llm();
        assert!(s.contains("rate_limited"));
        assert!(s.contains("retryable: true"));
        // RateLimited suggestion must mention retry but not promise it is automatic.
        let suggestion = ToolErrorCategory::RateLimited.suggestion();
        assert!(suggestion.contains("retry"), "{suggestion}");
        assert!(!suggestion.contains("automatically"), "{suggestion}");
    }

    #[test]
    fn transient_suggestion_neutral_no_automatically() {
        // Suggestion text must not promise "automatically" — retry may or may not fire
        // (executor may not be retryable, or retries may be exhausted).
        for cat in [
            ToolErrorCategory::ServerError,
            ToolErrorCategory::NetworkError,
            ToolErrorCategory::RateLimited,
            ToolErrorCategory::Timeout,
        ] {
            let s = cat.suggestion();
            assert!(
                !s.contains("automatically"),
                "{cat:?} suggestion must not promise automatic retry: {s}"
            );
        }
    }

    #[test]
    fn feedback_retryable_matches_category_is_retryable() {
        // Transient categories must produce retryable: true feedback.
        for cat in [
            ToolErrorCategory::ServerError,
            ToolErrorCategory::NetworkError,
            ToolErrorCategory::RateLimited,
            ToolErrorCategory::Timeout,
        ] {
            let fb = ToolErrorFeedback {
                category: cat,
                message: "error".to_owned(),
                retryable: cat.is_retryable(),
            };
            assert!(fb.retryable, "{cat:?} feedback must be retryable");
        }

        // Permanent categories must produce retryable: false feedback.
        for cat in [
            ToolErrorCategory::InvalidParameters,
            ToolErrorCategory::PolicyBlocked,
            ToolErrorCategory::PermanentFailure,
        ] {
            let fb = ToolErrorFeedback {
                category: cat,
                message: "error".to_owned(),
                retryable: cat.is_retryable(),
            };
            assert!(!fb.retryable, "{cat:?} feedback must not be retryable");
        }
    }

    // ── B4 regression: infrastructure errors must NOT be quality failures ─────

    #[test]
    fn b4_infrastructure_errors_not_quality_failures() {
        // These categories must never trigger self-reflection (B4 fix).
        for cat in [
            ToolErrorCategory::NetworkError,
            ToolErrorCategory::ServerError,
            ToolErrorCategory::RateLimited,
            ToolErrorCategory::Timeout,
        ] {
            assert!(
                !cat.is_quality_failure(),
                "{cat:?} must not be a quality failure"
            );
            // And they must be retryable.
            assert!(cat.is_retryable(), "{cat:?} must be retryable");
        }
    }

    #[test]
    fn b4_quality_failures_may_trigger_reflection() {
        // These categories should trigger self-reflection.
        for cat in [
            ToolErrorCategory::InvalidParameters,
            ToolErrorCategory::TypeMismatch,
            ToolErrorCategory::ToolNotFound,
        ] {
            assert!(
                cat.is_quality_failure(),
                "{cat:?} must be a quality failure"
            );
            // Quality failures are not retryable.
            assert!(!cat.is_retryable(), "{cat:?} must not be retryable");
        }
    }

    // ── ErrorDomain mapping: all 11 categories ───────────────────────────────

    #[test]
    fn domain_planning() {
        assert_eq!(
            ToolErrorCategory::ToolNotFound.domain(),
            ErrorDomain::Planning
        );
    }

    #[test]
    fn domain_reflection() {
        assert_eq!(
            ToolErrorCategory::InvalidParameters.domain(),
            ErrorDomain::Reflection
        );
        assert_eq!(
            ToolErrorCategory::TypeMismatch.domain(),
            ErrorDomain::Reflection
        );
    }

    #[test]
    fn domain_action() {
        for cat in [
            ToolErrorCategory::PolicyBlocked,
            ToolErrorCategory::ConfirmationRequired,
            ToolErrorCategory::PermanentFailure,
            ToolErrorCategory::Cancelled,
        ] {
            assert_eq!(
                cat.domain(),
                ErrorDomain::Action,
                "{cat:?} must map to Action"
            );
        }
    }

    #[test]
    fn domain_system() {
        for cat in [
            ToolErrorCategory::RateLimited,
            ToolErrorCategory::ServerError,
            ToolErrorCategory::NetworkError,
            ToolErrorCategory::Timeout,
        ] {
            assert_eq!(
                cat.domain(),
                ErrorDomain::System,
                "{cat:?} must map to System"
            );
        }
    }

    #[test]
    fn error_domain_helper_methods() {
        assert!(ErrorDomain::System.is_auto_retryable());
        assert!(!ErrorDomain::Planning.is_auto_retryable());
        assert!(!ErrorDomain::Reflection.is_auto_retryable());
        assert!(!ErrorDomain::Action.is_auto_retryable());

        assert!(ErrorDomain::Reflection.needs_llm_correction());
        assert!(ErrorDomain::Planning.needs_llm_correction());
        assert!(!ErrorDomain::System.needs_llm_correction());
        assert!(!ErrorDomain::Action.needs_llm_correction());
    }

    #[test]
    fn error_domain_labels() {
        assert_eq!(ErrorDomain::Planning.label(), "planning");
        assert_eq!(ErrorDomain::Reflection.label(), "reflection");
        assert_eq!(ErrorDomain::Action.label(), "action");
        assert_eq!(ErrorDomain::System.label(), "system");
    }

    // ── B2 regression: io::NotFound must NOT produce ToolNotFound ────────────

    #[test]
    fn b2_io_not_found_maps_to_permanent_failure_not_tool_not_found() {
        let err = std::io::Error::new(std::io::ErrorKind::NotFound, "bash: command not found");
        let cat = classify_io_error(&err);
        assert_ne!(
            cat,
            ToolErrorCategory::ToolNotFound,
            "OS-level NotFound must NOT map to ToolNotFound"
        );
        assert_eq!(
            cat,
            ToolErrorCategory::PermanentFailure,
            "OS-level NotFound must map to PermanentFailure"
        );
    }

    // ── ToolErrorCategory::Cancelled: not retryable, not quality failure ──────

    #[test]
    fn cancelled_is_not_retryable_and_not_quality_failure() {
        assert!(!ToolErrorCategory::Cancelled.is_retryable());
        assert!(!ToolErrorCategory::Cancelled.is_quality_failure());
        assert!(!ToolErrorCategory::Cancelled.needs_parameter_reformat());
    }

    // ── ToolInvocationPhase ──────────────────────────────────────────────────

    #[test]
    fn phase_setup_for_tool_not_found() {
        assert_eq!(
            ToolErrorCategory::ToolNotFound.phase(),
            ToolInvocationPhase::Setup
        );
    }

    #[test]
    fn phase_param_handling() {
        assert_eq!(
            ToolErrorCategory::InvalidParameters.phase(),
            ToolInvocationPhase::ParamHandling
        );
        assert_eq!(
            ToolErrorCategory::TypeMismatch.phase(),
            ToolInvocationPhase::ParamHandling
        );
    }

    #[test]
    fn phase_execution_for_runtime_errors() {
        for cat in [
            ToolErrorCategory::PolicyBlocked,
            ToolErrorCategory::ConfirmationRequired,
            ToolErrorCategory::PermanentFailure,
            ToolErrorCategory::Cancelled,
            ToolErrorCategory::RateLimited,
            ToolErrorCategory::ServerError,
            ToolErrorCategory::NetworkError,
            ToolErrorCategory::Timeout,
        ] {
            assert_eq!(cat.phase(), ToolInvocationPhase::Execution, "{cat:?}");
        }
    }

    #[test]
    fn phase_label_non_empty() {
        for phase in [
            ToolInvocationPhase::Setup,
            ToolInvocationPhase::ParamHandling,
            ToolInvocationPhase::Execution,
            ToolInvocationPhase::ResultInterpretation,
        ] {
            assert!(!phase.label().is_empty(), "{phase:?}");
        }
    }
}