loopctl 0.3.0

A trait-based framework for building agent loops with pluggable LLM clients, tools, and memory
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
//! Hook system — bidirectional lifecycle control for agent loops.
//!
//! Hooks differ from observers ([`crate::observer::LoopObserver`]) in two key ways:
//!
//! 1. **Return values matter.** Pre-hooks return [`HookAction`] (or [`CompactResult`])
//!    to control whether an action proceeds.
//! 2. **Short-circuit execution.** The executor stops at the first hook that returns
//!    a blocking result. Safety-critical hooks should be registered first.
//!
//! # Quick Start
//!
//! ```
//! use loopctl::hooks::{HookExecutor, Hook, HookAction};
//! use loopctl::hooks::context::PreToolUseContext;
//! use std::sync::Arc;
//!
//! struct BlockRmRf;
//! impl Hook for BlockRmRf {
//!     fn name(&self) -> &str { "block_rmrf" }
//!     fn on_pre_tool_use(&self, ctx: &PreToolUseContext) -> Option<HookAction> {
//!         if ctx.tool_name == "rm_rf" {
//!             Some(HookAction::block("rm -rf is not allowed"))
//!         } else {
//!             None
//!         }
//!     }
//! }
//!
//! let executor = HookExecutor::new()
//!     .with_hook(Arc::new(BlockRmRf));
//!
//! assert_eq!(executor.hook_count(), 1);
//! ```
//!
//! # Module Structure
//!
//! - **[`context`]** — Context types passed to hooks at each lifecycle point.
//! - **[`Hook`]** — The trait every hook implements.
//! - **[`HookExecutor`]** — Manages ordered hook execution with short-circuit semantics.
//! - **[`builtin`]** — Reference hook implementations (logging, blocklist, confirmation).

pub mod builtin;
pub mod context;
pub mod executor;

pub use executor::HookExecutor;

use std::fmt;

use context::{
    CompactResult, PostCompactContext, PostToolUseContext, PreCompactContext, PreToolUseContext,
    RunEndContext, RunStartContext,
};

/// Hook trait for bidirectional lifecycle control.
///
/// Hooks differ from observers ([`crate::observer::LoopObserver`]) in two key ways:
///
/// 1. **Return values matter.** `on_pre_*` methods return `Option<HookAction>` (or
///    `Option<CompactResult>`). Returning `Some(Block{...})` prevents the action
///    from proceeding.
///
/// 2. **Short-circuit execution.** The executor stops at the first hook that returns
///    a non-`None` / non-Allow result. Safety-critical hooks should be registered first.
///
/// # Synchronous Design
///
/// Hook methods are synchronous because hooks should be fast (no I/O, no network
/// calls). A hook that blocks a tool call does so by inspecting the tool name and
/// input — a pattern match, not a database lookup. If a hook needs async work
/// (e.g., calling a policy server), it should do that work eagerly at registration
/// time or maintain a local cache.
///
/// # Default Implementations
///
/// All methods have default no-op implementations returning `None` (allow).
/// Hooks only override the methods they care about.
///
/// # Examples
///
/// ```
/// use loopctl::hooks::{Hook, HookAction};
/// use loopctl::hooks::context::PreToolUseContext;
///
/// struct BlockDangerousTools;
///
/// impl Hook for BlockDangerousTools {
///     fn name(&self) -> &str { "block_dangerous" }
///
///     fn on_pre_tool_use(&self, ctx: &PreToolUseContext) -> Option<HookAction> {
///         if ctx.tool_name == "RmRF" {
///             Some(HookAction::block("rm -rf is not allowed"))
///         } else {
///             None
///         }
///     }
/// }
///
/// let hook = BlockDangerousTools;
/// assert_eq!(hook.name(), "block_dangerous");
/// ```
pub trait Hook: Send + Sync {
    /// Human-readable name for this hook.
    ///
    /// Used in logging and diagnostics to identify which hook
    /// produced a given result.
    fn name(&self) -> &str;

    /// Called before a tool is executed.
    ///
    /// Return `Some(HookAction::Block{...})` to prevent execution.
    /// Return `Some(HookAction::Ask{...})` to request interactive confirmation.
    /// Return `None` to allow (default).
    fn on_pre_tool_use(&self, _ctx: &PreToolUseContext) -> Option<HookAction> {
        None
    }

    /// Called before context compaction.
    ///
    /// Return `Some(CompactResult::abort(...))` to prevent compaction.
    /// Return `Some(CompactResult { new_instructions: Some(...), .. })` to
    /// inject custom instructions into the summarizer.
    /// Return `None` to allow (default).
    fn on_pre_compact(&self, _ctx: &PreCompactContext) -> Option<CompactResult> {
        None
    }

    /// Called after a tool completes execution.
    ///
    /// Cannot block. Use this for audit logging, metric recording,
    /// or triggering side effects.
    fn on_post_tool_use(&self, _ctx: &PostToolUseContext) {}

    /// Called after context compaction completes.
    ///
    /// Notification-only; cannot alter the compaction result.
    fn on_post_compact(&self, _ctx: &PostCompactContext) {}

    /// Called when a run starts.
    ///
    /// Fires at the start of each `run()` call. Use for initialization,
    /// credential setup, or state restoration.
    fn on_run_start(&self, _ctx: &RunStartContext) {}

    /// Called when a run ends.
    ///
    /// Fires at the end of each `run()` call. Use for cleanup, teardown,
    /// or final metric emission.
    fn on_run_end(&self, _ctx: &RunEndContext) {}
}

/// Whether the session can interact with a human operator.
///
/// This controls how [`HookAction::Ask`] is handled by the
/// [`HookExecutor`]:
///
/// - [`Interactivity::Headless`] — there is no human in the loop, so
///   `Ask` is automatically downgraded to `Block` with a descriptive
///   reason. Default and correct mode for autonomous
///   / headless agents (e.g. `BareLoop`).
/// - [`Interactivity::Interactive`] — a human is available to respond
///   to prompts, so `Ask` passes through unchanged.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum Interactivity {
    /// No human in the loop — `Ask` is downgraded to `Block`.
    #[default]
    Headless,
    /// Human is available — `Ask` passes through unchanged.
    Interactive,
}

/// Action returned by a pre-hook to control flow.
///
/// Pre-hooks return `Option<HookAction>`:
/// - `None` → allow (default)
/// - `Some(HookAction::Allow)` → explicitly allow
/// - `Some(HookAction::Block { reason })` → block with reason
/// - `Some(HookAction::Ask { message })` → request interactive confirmation
///
/// The executor short-circuits on the first non-`None` result that isn't `Allow`.
#[derive(Debug, Clone)]
pub enum HookAction {
    /// Allow the action to proceed.
    ///
    /// No intervention — the executor continues to the next hook
    /// or proceeds with the original action.
    Allow,

    /// Block the action with a human-readable reason.
    ///
    /// The reason is returned to the model as a tool error, allowing it
    /// to adjust its approach.
    Block {
        /// Why the action was blocked.
        ///
        /// Returned to the model as a tool error, allowing it
        /// to adjust its approach.
        reason: String,
    },

    /// Request interactive confirmation before proceeding.
    ///
    /// The executor delegates to a [`builtin::ConfirmationHandler`] to
    /// resolve the confirmation. If denied, the action is treated as
    /// blocked.
    Ask {
        /// Message to present to the user for confirmation.
        ///
        /// The executor delegates to a [`builtin::ConfirmationHandler`]
        /// to resolve the confirmation.
        message: String,
    },
}

impl fmt::Display for HookAction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Allow => write!(f, "Allow"),
            Self::Block { reason } => write!(f, "Block({reason})"),
            Self::Ask { message } => write!(f, "Ask({message})"),
        }
    }
}

impl HookAction {
    /// Convenience constructor for [`HookAction::Block`].
    ///
    /// Accepts any `Into<String>` so string literals work directly.
    /// The carried `reason` is returned to the model as a tool error,
    /// giving it a chance to adjust its approach. Prefer this over
    /// struct-literal construction at call sites that block.
    pub fn block(reason: impl Into<String>) -> Self {
        Self::Block {
            reason: reason.into(),
        }
    }

    /// Convenience constructor for [`HookAction::Ask`].
    ///
    /// Accepts any `Into<String>` so string literals work directly.
    /// The carried `message` is presented to the user for confirmation;
    /// the executor delegates to a
    /// [`ConfirmationHandler`](crate::hooks::builtin::ConfirmationHandler)
    /// to resolve it. In headless mode `Ask` is downgraded to `Block`
    /// by the [`HookExecutor`].
    pub fn ask(message: impl Into<String>) -> Self {
        Self::Ask {
            message: message.into(),
        }
    }

    /// Returns `true` if this action allows the operation to proceed.
    ///
    /// `true` only for [`HookAction::Allow`]. Use this in match arms
    /// where the default path is to proceed and only the block/ask
    /// branches need explicit handling.
    #[must_use]
    pub fn is_allow(&self) -> bool {
        matches!(self, Self::Allow)
    }

    /// Returns `true` if this action blocks the operation.
    ///
    /// `true` only for [`HookAction::Block`]. Note that in headless
    /// mode an [`HookAction::Ask`] is downgraded to `Block`, so a
    /// post-executor action that reports `is_block` may have originated
    /// as an `Ask`.
    #[must_use]
    pub fn is_block(&self) -> bool {
        matches!(self, Self::Block { .. })
    }

    /// Returns `true` if this action requests interactive confirmation.
    ///
    /// `true` only for [`HookAction::Ask`]. Note this reflects the
    /// action as produced by the hook, before the executor applies its
    /// interactivity policy — an `Ask` returned by a hook may surface
    /// as `Block` after the executor downgrades it in headless mode.
    #[must_use]
    pub fn is_ask(&self) -> bool {
        matches!(self, Self::Ask { .. })
    }

    /// Returns the block reason, if this is a [`HookAction::Block`].
    ///
    /// `Some(reason)` when this action is a `Block`, `None` for `Allow`
    /// and `Ask`. Useful for logging why an action was refused without
    /// a full match arm — pair with [`is_block`](Self::is_block) when
    /// you need to distinguish the variants.
    #[must_use]
    pub fn block_reason(&self) -> Option<&str> {
        match self {
            Self::Block { reason } => Some(reason),
            _ => None,
        }
    }
}

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

    #[test]
    fn hook_action_allow_is_allow() {
        assert!(HookAction::Allow.is_allow());
        assert!(!HookAction::Allow.is_block());
    }

    #[test]
    fn hook_action_block_factory() {
        let action = HookAction::block("dangerous");
        assert!(action.is_block());
        assert!(!action.is_allow());
        match action {
            HookAction::Block { reason } => assert_eq!(reason, "dangerous"),
            other => panic!("expected Block, got {other:?}"),
        }
    }

    #[test]
    fn hook_action_ask_factory() {
        let action = HookAction::ask("confirm?");
        match action {
            HookAction::Ask { message } => assert_eq!(message, "confirm?"),
            other => panic!("expected Ask, got {other:?}"),
        }
    }

    #[test]
    fn hook_action_display() {
        assert_eq!(HookAction::Allow.to_string(), "Allow");
        assert_eq!(HookAction::block("nope").to_string(), "Block(nope)");
        assert_eq!(HookAction::ask("y/n").to_string(), "Ask(y/n)");
    }

    #[test]
    fn block_reason_returns_none_for_allow() {
        assert_eq!(HookAction::Allow.block_reason(), None);
    }

    #[test]
    fn block_reason_returns_none_for_ask() {
        assert_eq!(
            HookAction::Ask {
                message: "ok?".into()
            }
            .block_reason(),
            None
        );
    }

    #[test]
    fn block_reason_returns_some_for_block() {
        assert_eq!(
            HookAction::Block { reason: "x".into() }.block_reason(),
            Some("x")
        );
    }

    #[test]
    fn is_allow_returns_false_for_block() {
        assert!(!HookAction::Block { reason: "x".into() }.is_allow());
    }

    #[test]
    fn is_allow_returns_false_for_ask() {
        assert!(
            !HookAction::Ask {
                message: "x".into()
            }
            .is_allow()
        );
    }

    #[test]
    fn is_block_returns_false_for_allow() {
        assert!(!HookAction::Allow.is_block());
    }

    #[test]
    fn is_block_returns_false_for_ask() {
        assert!(
            !HookAction::Ask {
                message: "x".into()
            }
            .is_block()
        );
    }

    #[test]
    fn is_ask_true_for_ask() {
        let action = HookAction::ask("proceed?");
        assert!(action.is_ask());
    }

    #[test]
    fn is_ask_false_for_block() {
        let action = HookAction::block("nope");
        assert!(!action.is_ask());
    }

    #[test]
    fn is_ask_false_for_allow() {
        assert!(!HookAction::Allow.is_ask());
    }

    #[test]
    fn interactivity_default_is_headless() {
        assert_eq!(Interactivity::default(), Interactivity::Headless);
    }

    #[test]
    fn interactivity_eq_semantics() {
        assert_eq!(Interactivity::Headless, Interactivity::Headless);
        assert_eq!(Interactivity::Interactive, Interactivity::Interactive);
        assert_ne!(Interactivity::Headless, Interactivity::Interactive);
    }
}