juncture 0.1.0

Typed state machine framework for LLM agents - Rust implementation of LangGraph
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
//! Agent-level middleware system for prebuilt agents.
//!
//! This module provides [`AgentMiddleware`] for intercepting agent execution
//! at the tool-call level, distinct from [`LlmMiddleware`](crate::llm::LlmMiddleware)
//! which wraps `ChatModel::invoke()`. Agent middleware can:
//!
//! - Transform state before/after model invocation
//! - Intercept tool calls before execution
//! - Handle errors across the entire agent loop
//! - Implement loop detection, guardrails, and observability
//!
//! # Lifecycle
//!
//! For each agent loop iteration:
//!
//! 1. [`AgentMiddleware::before_model()`] — transform state before LLM call
//! 2. LLM call executes
//! 3. [`AgentMiddleware::after_model()`] — transform model response
//! 4. If tool calls present:
//!    a. [`AgentMiddleware::before_tool()`] — intercept before tool execution
//!    b. Tool executes
//!    c. [`AgentMiddleware::after_tool()`] — intercept after tool execution
//! 5. [`AgentMiddleware::on_error()`] — handle errors at any stage

use std::fmt;
use std::sync::Arc;

use async_trait::async_trait;
use juncture_core::state::messages::Message;

use crate::prebuilt::messages_state::MessagesState;

/// Result of a middleware hook invocation.
///
/// Determines whether the agent loop should continue or short-circuit.
#[derive(Debug)]
pub enum MiddlewareAction {
    /// Continue with the normal agent loop.
    Continue,
    /// Short-circuit the current phase with a replacement message.
    /// For example, `before_model` can return a synthetic response to skip the LLM call.
    ShortCircuit(Message),
}

/// Agent-level middleware for intercepting agent execution.
///
/// Unlike [`LlmMiddleware`](crate::llm::LlmMiddleware) which wraps
/// individual `ChatModel::invoke()` calls, `AgentMiddleware` operates at the
/// agent loop level — intercepting tool calls, transforming state, and handling
/// errors across the entire execution cycle.
///
/// # Example
///
/// ```ignore
/// use juncture::prebuilt::middleware::{AgentMiddleware, MiddlewareAction};
/// use juncture::prebuilt::MessagesState;
/// use juncture::llm::Message;
///
/// struct LoggingMiddleware;
///
/// #[async_trait]
/// impl AgentMiddleware for LoggingMiddleware {
///     async fn before_model(&self, state: &MessagesState) -> MiddlewareAction {
///         tracing::info!("Agent has {} messages", state.messages.len());
///         MiddlewareAction::Continue
///     }
/// }
/// ```
#[async_trait]
pub trait AgentMiddleware: Send + Sync + fmt::Debug {
    /// Called before the model is invoked.
    ///
    /// Return [`MiddlewareAction::ShortCircuit`] with a synthetic message to skip
    /// the LLM call (e.g., for caching or guardrails).
    async fn before_model(&self, _state: &MessagesState) -> MiddlewareAction {
        MiddlewareAction::Continue
    }

    /// Called after the model returns a response.
    ///
    /// Return a modified message to replace the model's response.
    /// The default implementation returns the original message unchanged.
    async fn after_model(&self, _state: &MessagesState, response: &Message) -> Message {
        response.clone()
    }

    /// Called before a tool is executed.
    ///
    /// Return [`MiddlewareAction::ShortCircuit`] with a synthetic tool-result
    /// message to skip the actual tool execution (e.g., for permission checks).
    async fn before_tool(
        &self,
        _tool_name: &str,
        _arguments: &serde_json::Value,
    ) -> MiddlewareAction {
        MiddlewareAction::Continue
    }

    /// Called after a tool returns a result.
    ///
    /// Return a modified message to replace the tool's result.
    async fn after_tool(&self, _tool_name: &str, result: &Message) -> Message {
        result.clone()
    }

    /// Called when an error occurs at any stage of the agent loop.
    ///
    /// Return `Some(recovery_message)` to recover from the error and continue,
    /// or `None` to propagate the error.
    async fn on_error(&self, _error: &str) -> Option<Message> {
        None
    }
}

/// A no-op middleware that passes through all calls unchanged.
///
/// Useful as a pass-through default in middleware chains.
#[derive(Debug)]
pub struct NopMiddleware;

#[async_trait]
impl AgentMiddleware for NopMiddleware {}

/// Middleware that detects and prevents infinite tool loops.
///
/// Tracks consecutive identical tool calls and stops the agent when
/// the same tool is called with the same arguments more than `max_repetitions` times.
///
/// # Example
///
/// ```ignore
/// use juncture::prebuilt::middleware::LoopDetectionMiddleware;
///
/// let middleware = LoopDetectionMiddleware::new(3);
/// ```
#[derive(Debug)]
pub struct LoopDetectionMiddleware {
    max_repetitions: usize,
}

impl LoopDetectionMiddleware {
    /// Create a new loop detection middleware.
    ///
    /// # Arguments
    ///
    /// * `max_repetitions` - Maximum number of identical consecutive tool calls allowed.
    #[must_use]
    pub const fn new(max_repetitions: usize) -> Self {
        Self { max_repetitions }
    }
}

#[async_trait]
impl AgentMiddleware for LoopDetectionMiddleware {
    async fn before_model(&self, state: &MessagesState) -> MiddlewareAction {
        if state.messages.len() < self.max_repetitions * 2 {
            return MiddlewareAction::Continue;
        }

        // Check if the last N tool-call/result pairs are identical
        let recent: Vec<&Message> = state
            .messages
            .iter()
            .rev()
            .take(self.max_repetitions * 2)
            .collect();

        if recent.len() < self.max_repetitions * 2 {
            return MiddlewareAction::Continue;
        }

        // Check if all recent tool calls are identical
        let tool_calls: Vec<(&str, &serde_json::Value)> = recent
            .iter()
            .filter_map(|m| {
                m.tool_calls
                    .first()
                    .map(|tc| (tc.name.as_str(), &tc.arguments))
            })
            .collect();

        if tool_calls.len() >= self.max_repetitions {
            let first = &tool_calls[0];
            let all_same = tool_calls
                .iter()
                .all(|tc| tc.0 == first.0 && tc.1 == first.1);
            if all_same {
                return MiddlewareAction::ShortCircuit(Message::ai(format!(
                    "Loop detected: tool '{}' called {} times with identical arguments. Stopping.",
                    first.0, self.max_repetitions
                )));
            }
        }

        MiddlewareAction::Continue
    }
}

/// Middleware that handles tool execution errors gracefully.
///
/// Catches tool errors and returns a synthetic error message instead of
/// propagating the error, allowing the agent to recover and try alternative
/// approaches.
///
/// # Example
///
/// ```ignore
/// use juncture::prebuilt::middleware::ToolErrorHandlingMiddleware;
///
/// let middleware = ToolErrorHandlingMiddleware::new();
/// ```
#[derive(Debug)]
pub struct ToolErrorHandlingMiddleware;

impl ToolErrorHandlingMiddleware {
    /// Create a new tool error handling middleware.
    #[must_use]
    pub const fn new() -> Self {
        Self
    }
}

impl Default for ToolErrorHandlingMiddleware {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl AgentMiddleware for ToolErrorHandlingMiddleware {
    async fn after_tool(&self, tool_name: &str, result: &Message) -> Message {
        // Check if the result indicates an error
        if let crate::llm::Content::Text(text) = &result.content
            && (text.starts_with("Error:") || text.starts_with("error:"))
        {
            return Message::tool_result(
                result.tool_call_id.clone().unwrap_or_default(),
                format!(
                    "Tool '{tool_name}' failed: {text}\nPlease try a different approach or tool."
                ),
            );
        }
        result.clone()
    }

    async fn on_error(&self, error: &str) -> Option<Message> {
        Some(Message::ai(format!(
            "An error occurred: {error}\nLet me try a different approach."
        )))
    }
}

/// Container for an ordered chain of agent middleware.
///
/// Middleware are executed in the order they were added for `before_*` hooks,
/// and in reverse order for `after_*` hooks (last added runs first).
///
/// # Example
///
/// ```ignore
/// use juncture::prebuilt::middleware::{
///     AgentMiddlewareChain, LoopDetectionMiddleware, ToolErrorHandlingMiddleware,
/// };
///
/// let chain = AgentMiddlewareChain::new()
///     .with(LoopDetectionMiddleware::new(3))
///     .with(ToolErrorHandlingMiddleware::new());
/// ```
#[derive(Clone)]
pub struct AgentMiddlewareChain {
    middlewares: Vec<Arc<dyn AgentMiddleware>>,
}

impl AgentMiddlewareChain {
    /// Create an empty middleware chain.
    #[must_use]
    pub fn new() -> Self {
        Self {
            middlewares: Vec::new(),
        }
    }

    /// Add a middleware to the chain.
    ///
    /// Middleware are executed in insertion order for `before_*` hooks,
    /// and in reverse order for `after_*` hooks.
    #[must_use]
    pub fn with<M: AgentMiddleware + 'static>(mut self, middleware: M) -> Self {
        self.middlewares.push(Arc::new(middleware));
        self
    }

    /// Get the number of middleware in the chain.
    #[must_use]
    pub fn len(&self) -> usize {
        self.middlewares.len()
    }

    /// Check if the chain is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.middlewares.is_empty()
    }

    /// Run all `before_model` hooks in forward order.
    ///
    /// Returns the first `ShortCircuit` encountered, or `Continue` if all pass.
    pub async fn run_before_model(&self, state: &MessagesState) -> MiddlewareAction {
        for mw in &self.middlewares {
            match mw.before_model(state).await {
                MiddlewareAction::Continue => {}
                MiddlewareAction::ShortCircuit(msg) => return MiddlewareAction::ShortCircuit(msg),
            }
        }
        MiddlewareAction::Continue
    }

    /// Run all `after_model` hooks in reverse order.
    pub async fn run_after_model(&self, state: &MessagesState, response: &Message) -> Message {
        let mut result = response.clone();
        for mw in self.middlewares.iter().rev() {
            result = mw.after_model(state, &result).await;
        }
        result
    }

    /// Run all `before_tool` hooks in forward order.
    pub async fn run_before_tool(
        &self,
        tool_name: &str,
        arguments: &serde_json::Value,
    ) -> MiddlewareAction {
        for mw in &self.middlewares {
            match mw.before_tool(tool_name, arguments).await {
                MiddlewareAction::Continue => {}
                MiddlewareAction::ShortCircuit(msg) => return MiddlewareAction::ShortCircuit(msg),
            }
        }
        MiddlewareAction::Continue
    }

    /// Run all `after_tool` hooks in reverse order.
    pub async fn run_after_tool(&self, tool_name: &str, result: &Message) -> Message {
        let mut result = result.clone();
        for mw in self.middlewares.iter().rev() {
            result = mw.after_tool(tool_name, &result).await;
        }
        result
    }

    /// Run all `on_error` hooks until one returns a recovery message.
    pub async fn run_on_error(&self, error: &str) -> Option<Message> {
        for mw in &self.middlewares {
            if let Some(recovery) = mw.on_error(error).await {
                return Some(recovery);
            }
        }
        None
    }
}

impl Default for AgentMiddlewareChain {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Debug for AgentMiddlewareChain {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AgentMiddlewareChain")
            .field("count", &self.middlewares.len())
            .finish()
    }
}

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

    #[test]
    fn test_nop_middleware_debug() {
        let mw = NopMiddleware;
        let debug = format!("{mw:?}");
        assert_eq!(debug, "NopMiddleware");
    }

    #[test]
    fn test_loop_detection_middleware_new() {
        let mw = LoopDetectionMiddleware::new(3);
        assert_eq!(mw.max_repetitions, 3);
    }

    #[test]
    fn test_tool_error_handling_middleware_default() {
        let mw = ToolErrorHandlingMiddleware;
        let _ = format!("{mw:?}");
    }

    #[test]
    fn test_middleware_chain_new() {
        let chain = AgentMiddlewareChain::new();
        assert!(chain.is_empty());
        assert_eq!(chain.len(), 0);
    }

    #[test]
    fn test_middleware_chain_with() {
        let chain = AgentMiddlewareChain::new()
            .with(NopMiddleware)
            .with(LoopDetectionMiddleware::new(3));
        assert_eq!(chain.len(), 2);
        assert!(!chain.is_empty());
    }

    #[test]
    fn test_middleware_chain_default() {
        let chain = AgentMiddlewareChain::default();
        assert!(chain.is_empty());
    }

    #[test]
    fn test_middleware_chain_clone() {
        let chain = AgentMiddlewareChain::new().with(NopMiddleware);
        let cloned = chain.clone();
        drop(chain);
        assert_eq!(cloned.len(), 1);
    }

    #[test]
    fn test_middleware_chain_debug() {
        let chain = AgentMiddlewareChain::new().with(NopMiddleware);
        let debug = format!("{chain:?}");
        assert!(debug.contains("AgentMiddlewareChain"));
        assert!(debug.contains("count: 1"));
    }

    #[tokio::test]
    async fn test_middleware_action_debug() {
        let cont = MiddlewareAction::Continue;
        assert_eq!(format!("{cont:?}"), "Continue");

        let sc = MiddlewareAction::ShortCircuit(Message::ai("test"));
        let debug = format!("{sc:?}");
        assert!(debug.contains("ShortCircuit"));
    }

    #[tokio::test]
    async fn test_chain_run_before_model_continue() {
        let chain = AgentMiddlewareChain::new().with(NopMiddleware);
        let state = MessagesState::default();
        let result = chain.run_before_model(&state).await;
        assert!(matches!(result, MiddlewareAction::Continue));
    }

    #[tokio::test]
    async fn test_chain_run_after_model_passthrough() {
        let chain = AgentMiddlewareChain::new().with(NopMiddleware);
        let state = MessagesState::default();
        let response = Message::ai("hello");
        let result = chain.run_after_model(&state, &response).await;
        assert_eq!(result.content_text(), "hello");
    }

    #[tokio::test]
    async fn test_chain_run_before_tool_continue() {
        let chain = AgentMiddlewareChain::new().with(NopMiddleware);
        let args = serde_json::json!({});
        let result = chain.run_before_tool("test_tool", &args).await;
        assert!(matches!(result, MiddlewareAction::Continue));
    }

    #[tokio::test]
    async fn test_chain_run_after_tool_passthrough() {
        let chain = AgentMiddlewareChain::new().with(NopMiddleware);
        let result_msg = Message::tool_result("call_1", "result");
        let result = chain.run_after_tool("test_tool", &result_msg).await;
        assert_eq!(result.content_text(), "result");
    }

    #[tokio::test]
    async fn test_chain_run_on_error_none() {
        let chain = AgentMiddlewareChain::new().with(NopMiddleware);
        let result = chain.run_on_error("test error").await;
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_tool_error_handling_recovery() {
        let chain = AgentMiddlewareChain::new().with(ToolErrorHandlingMiddleware::new());
        let result = chain.run_on_error("something broke").await;
        assert!(result.is_some());
        let msg = result.unwrap();
        assert!(msg.content_text().contains("something broke"));
    }

    #[tokio::test]
    async fn test_tool_error_handling_normal_result() {
        let chain = AgentMiddlewareChain::new().with(ToolErrorHandlingMiddleware::new());
        let result_msg = Message::tool_result("call_1", "success");
        let result = chain.run_after_tool("test_tool", &result_msg).await;
        assert_eq!(result.content_text(), "success");
    }

    #[tokio::test]
    async fn test_tool_error_handling_error_result() {
        let chain = AgentMiddlewareChain::new().with(ToolErrorHandlingMiddleware::new());
        let result_msg = Message::tool_result("call_1", "Error: something failed");
        let result = chain.run_after_tool("test_tool", &result_msg).await;
        assert!(result.content_text().contains("test_tool"));
        assert!(result.content_text().contains("Error: something failed"));
    }

    #[tokio::test]
    async fn test_loop_detection_no_loop() {
        let chain = AgentMiddlewareChain::new().with(LoopDetectionMiddleware::new(3));
        let state = MessagesState {
            messages: vec![Message::human("hello")],
        };
        let result = chain.run_before_model(&state).await;
        assert!(matches!(result, MiddlewareAction::Continue));
    }
}

// Rust guideline compliant 2026-05-27