beleth 0.2.0-rc.1

Autonomous agent framework - The King commands legions
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
//! Core types for the agentic loop.
//!
//! These types model the agentic loop's state machine, configuration,
//! termination conditions, meta-signals, and autonomy grants.
//!
//! Reference: AGENTIC-LOOP-SPEC.md v0.1.0

use std::time::Duration;

use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// Loop State Machine
// ---------------------------------------------------------------------------

/// States of the agentic loop state machine.
///
/// ```text
/// Initialized → Generating → Detecting → Executing → Integrating → (continue → Generating)
///                              ↓             ↓
///                           Completed      (back to Detecting on exec complete)
///                           Stuck
///                           Yielded
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum LoopState {
    /// Loop created, not yet started.
    Initialized,
    /// Generating model output.
    Generating,
    /// Analyzing output for tool calls and meta-signals.
    Detecting,
    /// Executing detected tool calls.
    Executing,
    /// Integrating tool results into context.
    Integrating,
    /// Agent provided a final answer — terminal.
    Completed,
    /// Agent declared it is stuck — terminal.
    Stuck,
    /// Agent yielded to another agent — terminal.
    Yielded,
}

impl LoopState {
    /// Returns `true` if this is a terminal state (no further transitions allowed).
    pub fn is_terminal(self) -> bool {
        matches!(self, Self::Completed | Self::Stuck | Self::Yielded)
    }
}

/// High-level status of the loop, separate from the mechanical state.
///
/// Describes *how* the agent is doing, not *where* it is in the state machine.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum LoopStatus {
    /// Making forward progress.
    Progressing,
    /// Trying approaches, not yet converged.
    Exploring,
    /// Attempts not yielding results, but not stuck.
    Struggling,
    /// Explicitly requested help.
    Stuck,
    /// Finished.
    Completed,
    /// Externally stopped.
    Terminated,
}

// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------

/// Configuration for the agentic loop.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoopConfig {
    /// Maximum iterations before resource termination. Default: 10.
    pub max_iterations: u32,
    /// Maximum total tool calls. Default: 50.
    pub max_tool_calls: u32,
    /// Maximum wall-clock time. Default: 5 minutes.
    pub max_wall_time: Duration,
    /// Maximum tokens the model may generate across all iterations. Default: 16384.
    pub max_tokens: u32,
    /// Enable context compression when budget is tight. Default: true.
    pub context_compression: bool,
    /// Allow the agent to express uncertainty. Default: true.
    pub allow_uncertainty: bool,
    /// Allow the agent to yield to another agent. Default: true.
    pub allow_yield: bool,
    /// Preserve exploration history on termination. Default: true.
    pub preserve_exploration: bool,
    /// Detect implicit meta-signals from natural language. Default: true.
    pub detect_implicit_signals: bool,
    /// Timeout for tool approval requests. Default: 5 minutes.
    pub approval_timeout: Duration,
}

impl Default for LoopConfig {
    fn default() -> Self {
        Self {
            max_iterations: 10,
            max_tool_calls: 50,
            max_wall_time: Duration::from_secs(300),
            max_tokens: 16384,
            context_compression: true,
            allow_uncertainty: true,
            allow_yield: true,
            preserve_exploration: true,
            detect_implicit_signals: true,
            approval_timeout: Duration::from_secs(300),
        }
    }
}

// ---------------------------------------------------------------------------
// Token Budget
// ---------------------------------------------------------------------------

/// Tracks token usage across the loop's lifetime.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenBudget {
    /// Maximum tokens allowed.
    pub limit: u32,
    /// Tokens generated so far.
    pub used: u32,
}

impl TokenBudget {
    /// Creates a new budget with the given limit.
    pub fn new(limit: u32) -> Self {
        Self { limit, used: 0 }
    }

    /// Returns remaining tokens.
    pub fn remaining(&self) -> u32 {
        self.limit.saturating_sub(self.used)
    }

    /// Returns true if the budget is exhausted.
    pub fn is_exhausted(&self) -> bool {
        self.used >= self.limit
    }

    /// Records token usage. Returns `true` if still within budget.
    pub fn consume(&mut self, tokens: u32) -> bool {
        self.used = self.used.saturating_add(tokens);
        !self.is_exhausted()
    }
}

// ---------------------------------------------------------------------------
// Termination
// ---------------------------------------------------------------------------

/// Why the loop terminated.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TerminationReason {
    /// Agent terminated naturally (answer, stuck, yield, task complete).
    Natural(NaturalTermination),
    /// Resource limit reached.
    Resource(ResourceTermination),
    /// External intervention.
    External(ExternalTermination),
}

/// Natural termination — the agent decided to stop.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum NaturalTermination {
    /// Agent provided a final answer.
    AnswerProvided {
        /// The answer content.
        answer: String,
        /// Agent's self-assessed confidence (0.0–1.0).
        confidence: f32,
    },
    /// Agent yielded to another agent.
    AgentYielded {
        /// Partial progress, if any.
        partial: Option<String>,
        /// Why the agent is yielding.
        reason: String,
    },
    /// Agent is stuck and requested help.
    AgentStuck {
        /// Number of approaches attempted.
        attempts: u32,
        /// What kind of help is needed.
        request: StuckRequest,
    },
    /// Task finished with no more tool calls or answers.
    TaskComplete,
}

/// Resource termination — a limit was reached.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ResourceTermination {
    /// Hit iteration limit.
    MaxIterations {
        /// Iterations completed.
        completed: u32,
        /// Configured limit.
        limit: u32,
    },
    /// Hit token budget.
    TokenBudgetExhausted {
        /// Tokens generated.
        generated: u32,
        /// Configured budget.
        budget: u32,
    },
    /// Hit wall time.
    WallTimeExceeded {
        /// Elapsed time.
        elapsed: Duration,
        /// Configured limit.
        limit: Duration,
    },
    /// Hit tool call limit.
    ToolCallLimitReached {
        /// Tool calls made.
        calls: u32,
        /// Configured limit.
        limit: u32,
    },
}

/// External termination — someone else stopped us.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExternalTermination {
    /// Client cancelled the request.
    ClientCancelled,
    /// Operator intervention.
    OperatorTerminated {
        /// Reason for termination.
        reason: String,
    },
    /// System shutting down.
    SystemShutdown,
}

// ---------------------------------------------------------------------------
// Meta-Signals
// ---------------------------------------------------------------------------

/// Signals emitted by the agent beyond tool calls.
///
/// These communicate agent state to the loop orchestrator.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MetaSignal {
    /// "I have an answer."
    Answer {
        /// The answer content.
        content: String,
        /// Self-assessed confidence (0.0–1.0).
        confidence: f32,
        /// Caveats or assumptions.
        caveats: Vec<String>,
    },
    /// "I'm not certain."
    Uncertain {
        /// Partial answer, if any.
        partial_answer: Option<String>,
        /// Information the agent is missing.
        missing_information: Vec<String>,
        /// What would help the agent.
        would_help: Vec<String>,
    },
    /// "I've tried but I'm stuck."
    Stuck {
        /// Approaches the agent has tried.
        attempts: Vec<AttemptSummary>,
        /// Agent's hypothesis about the problem.
        hypothesis: Option<String>,
        /// What kind of help is needed.
        request: StuckRequest,
    },
    /// "Another agent might do better."
    Yield {
        /// Partial progress so far.
        partial_progress: Option<String>,
        /// What expertise would help.
        suggested_expertise: Vec<String>,
    },
    /// "I need to think about this."
    Thinking {
        /// What the agent is considering.
        direction: String,
        /// Estimated remaining steps.
        estimated_steps: Option<u32>,
    },
}

/// Summary of a failed approach the agent tried.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttemptSummary {
    /// Description of what was tried.
    pub description: String,
    /// Why it didn't work.
    pub outcome: String,
}

/// What the stuck agent is requesting.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StuckRequest {
    /// Needs clarification — has specific questions.
    Clarification(Vec<String>),
    /// Needs more context about a topic.
    MoreContext {
        /// What the agent needs context about.
        about: String,
    },
    /// Needs different tools than what's available.
    DifferentTools {
        /// What capabilities are needed.
        need: Vec<String>,
    },
    /// Needs a human to intervene.
    HumanIntervention {
        /// Why human help is needed.
        reason: String,
    },
}

/// Configuration for meta-signal detection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetectionConfig {
    /// Whether to detect implicit signals from natural language patterns.
    pub detect_implicit: bool,
    /// Minimum confidence for implicit detection (0.0–1.0).
    pub implicit_threshold: f32,
}

impl Default for DetectionConfig {
    fn default() -> Self {
        Self {
            detect_implicit: true,
            implicit_threshold: 0.7,
        }
    }
}

// ---------------------------------------------------------------------------
// Autonomy
// ---------------------------------------------------------------------------

/// Defines what the agent can do without asking permission.
///
/// Principle 5: Agents work best when they know their constraints upfront.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AutonomyGrant {
    /// Patterns that are auto-approved (no confirmation needed).
    pub auto_approve: Vec<ToolPattern>,
    /// Patterns that require explicit approval before execution.
    pub require_approval: Vec<ToolPattern>,
    /// Patterns that are forbidden entirely.
    pub forbidden: Vec<ToolPattern>,
}

/// A pattern that matches tool calls.
///
/// Uses glob-style matching for file paths and command strings.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ToolPattern {
    /// Matches read-type tools with the given path glob.
    Read(String),
    /// Matches write-type tools with the given path glob.
    Write(String),
    /// Matches bash/shell tools with the given command glob.
    Bash(String),
    /// Matches any tool by name.
    Tool(String),
}

/// Result of checking a tool call against an autonomy grant.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Permission {
    /// Tool is auto-approved.
    Allowed,
    /// Tool requires explicit approval.
    RequiresApproval,
    /// Tool is forbidden.
    Forbidden,
}

/// Outcome of attempting to execute a tool call.
#[derive(Debug, Clone)]
pub enum ExecutionOutcome {
    /// Tool executed successfully.
    Completed(AgenticToolResult),
    /// Tool call is forbidden by autonomy grant.
    Denied {
        /// Why it was denied.
        reason: String,
    },
    /// Tool call needs approval before proceeding.
    PendingApproval {
        /// The tool call awaiting approval.
        call_id: String,
    },
    /// Tool execution failed.
    Failed(ToolError),
}

// ---------------------------------------------------------------------------
// Tool Results (namespaced to avoid conflict with beleth::tool::ToolResult)
// ---------------------------------------------------------------------------

/// Structured tool result for agentic loop consumption.
///
/// Richer than `beleth::tool::ToolResult` — designed for agent reasoning.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgenticToolResult {
    /// ID linking back to the tool call.
    pub call_id: String,
    /// Which tool was called.
    pub tool_name: String,
    /// Outcome status.
    pub status: ResultStatus,
    /// Structured result data.
    pub data: serde_json::Value,
    /// How confident is the result.
    pub confidence: Confidence,
    /// How long the tool took (milliseconds).
    pub latency_ms: u64,
    /// Whether the output was truncated.
    pub truncated: bool,
}

/// Status of a tool execution result.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ResultStatus {
    /// Tool succeeded fully.
    Success,
    /// Tool partially succeeded.
    PartialSuccess {
        /// Number of operations that completed.
        completed: u32,
        /// Number of operations that failed.
        failed: u32,
    },
    /// Tool succeeded but found nothing (distinct from error).
    Empty,
    /// Tool failed.
    Failed {
        /// Whether the failure is recoverable (retry might help).
        recoverable: bool,
    },
}

/// How confident the tool result is.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum Confidence {
    /// Result is exact (file contents, API response).
    Measured,
    /// Result is computed/inferred.
    Estimated,
    /// Result may be incomplete or stale.
    Uncertain,
    /// Confidence cannot be determined.
    Unknown,
}

/// Error from tool execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolError {
    /// ID linking back to the tool call.
    pub call_id: String,
    /// Error description.
    pub error: String,
    /// Whether retrying might succeed.
    pub recoverable: bool,
    /// Suggestion for the agent.
    pub suggestion: Option<String>,
}

// ---------------------------------------------------------------------------
// Exploration Tracking
// ---------------------------------------------------------------------------

/// A branch of exploration the agent pursued.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExplorationBranch {
    /// Human/agent-readable description of this approach.
    pub description: String,
    /// Tool calls made during this exploration.
    pub tool_calls: Vec<String>,
    /// Whether this branch yielded useful results.
    pub productive: bool,
    /// Summary of findings, if any.
    pub findings: Option<String>,
}

// ---------------------------------------------------------------------------
// Loop Summary
// ---------------------------------------------------------------------------

/// Summary returned when the loop terminates, preserving partial work.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoopSummary {
    /// Why the loop terminated.
    pub termination: TerminationReason,
    /// Number of iterations completed.
    pub iterations_completed: u32,
    /// Total tool calls made.
    pub tool_calls_made: u32,
    /// Total tokens generated.
    pub tokens_generated: u32,
    /// Total wall-clock time.
    pub wall_time: Duration,
    /// Partial answer if the agent didn't finish.
    pub partial_answer: Option<String>,
    /// Exploration branches pursued.
    pub exploration_summary: Vec<ExplorationBranch>,
    /// Summary of all tool results.
    pub tool_results_summary: Vec<AgenticToolResult>,
    /// Whether the loop can be resumed.
    pub can_resume: bool,
    /// Continuation token for resuming (present when `can_resume` is `true`).
    ///
    /// Pass this token to the continuation API to resume the loop with
    /// additional context or extended resource budgets.
    pub continuation_token: Option<String>,
}

// ---------------------------------------------------------------------------
// Context Window
// ---------------------------------------------------------------------------

/// Manages the context window for the agentic loop.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextWindow {
    /// Conversation messages.
    pub messages: Vec<ContextMessage>,
    /// Current loop state visible to the agent.
    pub system_state: LoopStateSnapshot,
    /// Original token count before any compression.
    pub original_token_count: u32,
    /// Current token count after compression.
    pub current_token_count: u32,
    /// Compression events applied.
    pub compressions_applied: Vec<CompressionEvent>,
}

/// A message in the agentic context window.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextMessage {
    /// Role (system, user, assistant, tool).
    pub role: String,
    /// Content of the message.
    pub content: String,
    /// Tool call ID, if this is a tool result.
    pub tool_call_id: Option<String>,
}

/// Snapshot of loop state visible to the agent.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoopStateSnapshot {
    /// Current iteration number.
    pub iteration: u32,
    /// Maximum iterations allowed.
    pub max_iterations: u32,
    /// Remaining token budget.
    pub token_budget_remaining: u32,
    /// Names of available tools.
    pub tools_available: Vec<String>,
    /// Context pressure (0.0–1.0), how full the context window is.
    pub context_pressure: f32,
}

/// How context was compressed.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CompressionStrategy {
    /// Summarize old tool results, keep recent ones verbatim.
    SummarizeOldResults {
        /// Number of recent results to keep verbatim.
        keep_recent: u32,
    },
    /// Keep only tool results that led to progress.
    PruneDeadEnds,
    /// Compress exploration branches that didn't pan out.
    CollapseExploration {
        /// Token budget for summaries.
        summary_tokens: u32,
    },
    /// Ask the agent what to keep.
    AgentDirected,
}

/// Record of a compression event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressionEvent {
    /// Strategy used.
    pub strategy: CompressionStrategy,
    /// Tokens saved.
    pub tokens_saved: u32,
    /// Iteration at which compression occurred.
    pub at_iteration: u32,
}

// ---------------------------------------------------------------------------
// SSE Streaming Events
// ---------------------------------------------------------------------------

/// Events streamed to the client throughout loop execution.
///
/// Reference: AGENTIC-LOOP-SPEC.md §5.1
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "event", content = "data")]
#[serde(rename_all = "snake_case")]
pub enum LoopEvent {
    // -- Loop lifecycle --
    /// Loop has started.
    LoopStarted {
        /// Session identifier.
        session_id: String,
        /// Loop configuration.
        config: LoopConfig,
    },
    /// A new iteration has begun.
    IterationStarted {
        /// Iteration number.
        iteration: u32,
        /// Current loop status.
        status: LoopStatus,
    },
    /// An iteration has completed.
    IterationCompleted {
        /// Iteration number.
        iteration: u32,
        /// What happened in this iteration.
        outcome: IterationOutcome,
    },
    /// Loop has finished.
    LoopCompleted {
        /// Final summary.
        summary: LoopSummary,
    },

    // -- Generation --
    /// A token was generated.
    TokenGenerated {
        /// The token text.
        token: String,
    },
    /// Generation phase completed.
    GenerationCompleted {
        /// Full generated content.
        content: String,
        /// Token count.
        tokens: u32,
    },

    // -- Tool execution --
    /// Tool call detected in model output.
    ToolCallDetected {
        /// Tool call ID.
        call_id: String,
        /// Tool name.
        tool: String,
    },
    /// Tool execution started.
    ToolExecutionStarted {
        /// Tool call ID.
        call_id: String,
        /// Tool name.
        tool: String,
    },
    /// Tool execution completed.
    ToolExecutionCompleted {
        /// Tool call ID.
        call_id: String,
        /// Tool result.
        result: AgenticToolResult,
    },
    /// Tool requires approval before execution.
    ///
    /// Extended per AGENTIC-LOOP-SPEC §9.4.5 to include arguments and timeout
    /// so the client can make an informed decision.
    ToolApprovalRequired {
        /// Tool call ID.
        call_id: String,
        /// Tool name.
        tool: String,
        /// Tool call arguments (so the client can inspect what will run).
        arguments: serde_json::Value,
        /// Approval timeout in seconds.
        timeout_secs: u64,
        /// Number of currently pending approval requests (including this one).
        pending_count: usize,
    },

    // -- Meta-signals --
    /// Meta-signal detected in model output.
    MetaSignalDetected {
        /// The detected signal.
        signal: MetaSignal,
    },

    // -- Context management --
    /// Context was compressed to fit budget.
    ContextCompressed {
        /// Strategy used.
        strategy: CompressionStrategy,
        /// Tokens saved.
        saved_tokens: u32,
    },

    // -- Errors --
    /// An error occurred (system issue, not agent failure).
    Error {
        /// Error message.
        message: String,
        /// Whether the error is recoverable.
        recoverable: bool,
    },
}

/// What happened during an iteration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum IterationOutcome {
    /// Tool calls were executed.
    ToolCallsExecuted {
        /// Number of tool calls.
        count: u32,
    },
    /// Agent provided an answer.
    AnswerProvided,
    /// Agent is stuck.
    Stuck,
    /// Agent yielded.
    Yielded,
    /// Iteration was cut short by resource limit.
    ResourceLimitReached,
}

// ---------------------------------------------------------------------------
// Transition Errors
// ---------------------------------------------------------------------------

/// Errors from invalid state transitions.
#[derive(Debug, Clone, thiserror::Error)]
pub enum TransitionError {
    /// Attempted an invalid state transition.
    #[error("invalid transition from {from:?}: {reason}")]
    InvalidTransition {
        /// Current state.
        from: LoopState,
        /// Why the transition is invalid.
        reason: String,
    },
    /// A resource limit was reached.
    #[error("resource limit: {0}")]
    ResourceLimitReached(String),
    /// Loop has already terminated.
    #[error("loop already terminated")]
    AlreadyTerminated,
}