brainwires-agents 0.8.0

Agent orchestration, coordination, and lifecycle management for the Brainwires Agent Framework
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
//! Agent Loop Hooks - Granular control over the agent execution loop
//!
//! Provides [`AgentLifecycleHooks`] for intercepting every phase of the agent
//! loop: iteration boundaries, provider calls, tool execution, completion, and
//! context management.
//!
//! Unlike the observational [`LifecycleEvent`][brainwires_core::lifecycle::LifecycleEvent]
//! system in `brainwires-core`, these hooks can **control** the loop — skip
//! iterations, override tool results, delegate work to sub-agents, or compress
//! conversation history.
//!
//! # Usage
//!
//! ```rust,ignore
//! use brainwires_agents::agent_hooks::*;
//! use brainwires_agents::AgentContext;
//!
//! struct MyHooks;
//!
//! #[async_trait::async_trait]
//! impl AgentLifecycleHooks for MyHooks {
//!     async fn on_before_tool_execution(
//!         &self,
//!         _ctx: &IterationContext<'_>,
//!         tool_use: &ToolUse,
//!     ) -> ToolDecision {
//!         if tool_use.name == "write_file" {
//!             ToolDecision::Delegate(Box::new(DelegationRequest {
//!                 task_description: format!("Write file: {:?}", tool_use.input),
//!                 ..Default::default()
//!             }))
//!         } else {
//!             ToolDecision::Execute
//!         }
//!     }
//! }
//!
//! let context = AgentContext::new(/* ... */)
//!     .with_lifecycle_hooks(Arc::new(MyHooks));
//! ```

use std::sync::Arc;
use std::time::Duration;

use anyhow::Result;
use async_trait::async_trait;

use brainwires_core::{Message, Role, ToolResult, ToolUse, estimate_tokens_from_size};

use crate::pool::AgentPool;
use crate::task_agent::TaskAgentConfig;

// ── Iteration context ────────────────────────────────────────────────────────

/// Read-only snapshot of the current iteration state, passed to every hook.
#[derive(Debug, Clone)]
pub struct IterationContext<'a> {
    /// The agent's unique identifier.
    pub agent_id: &'a str,
    /// Current iteration number (1-based).
    pub iteration: u32,
    /// Maximum iterations allowed.
    pub max_iterations: u32,
    /// Cumulative tokens consumed so far.
    pub total_tokens_used: u64,
    /// Cumulative estimated cost in USD.
    pub total_cost_usd: f64,
    /// Wall-clock time since execution started.
    pub elapsed: Duration,
    /// Number of messages in the conversation history.
    pub conversation_len: usize,
}

// ── Decision enums ───────────────────────────────────────────────────────────

/// Decision returned by [`AgentLifecycleHooks::on_before_iteration`].
#[derive(Debug, Clone)]
pub enum IterationDecision {
    /// Proceed with the normal iteration.
    Continue,
    /// Skip the provider call this iteration. Use when the hook already
    /// injected messages or handled the iteration itself.
    Skip,
    /// Abort the agent loop with a failure message.
    Abort(String),
}

/// Decision returned by [`AgentLifecycleHooks::on_before_tool_execution`].
#[derive(Debug, Clone)]
pub enum ToolDecision {
    /// Execute the tool normally.
    Execute,
    /// Skip execution and inject this result instead.
    Override(ToolResult),
    /// Delegate the tool call to a sub-agent.
    Delegate(Box<DelegationRequest>),
}

// ── Delegation types ─────────────────────────────────────────────────────────

/// A request to delegate work to a sub-agent.
#[derive(Debug, Clone)]
pub struct DelegationRequest {
    /// Description of the sub-task for the spawned agent.
    pub task_description: String,
    /// Optional config override for the sub-agent.
    pub config: Option<TaskAgentConfig>,
    /// Messages to seed the sub-agent's conversation with.
    pub seed_messages: Vec<Message>,
    /// If `true`, block until the sub-agent completes and return its output.
    /// If `false`, the framework injects a placeholder and continues.
    pub blocking: bool,
}

impl Default for DelegationRequest {
    fn default() -> Self {
        Self {
            task_description: String::new(),
            config: None,
            seed_messages: Vec::new(),
            blocking: true,
        }
    }
}

/// Result of a completed delegation.
#[derive(Debug, Clone)]
pub struct DelegationResult {
    /// The sub-agent's unique ID.
    pub agent_id: String,
    /// Whether the sub-agent completed successfully.
    pub success: bool,
    /// Output summary from the sub-agent.
    pub output: String,
    /// Iterations the sub-agent consumed.
    pub iterations_used: u32,
    /// Tokens the sub-agent consumed.
    pub tokens_used: u64,
}

// ── Conversation view ────────────────────────────────────────────────────────

/// Controlled read/write handle to the conversation history.
///
/// Passed to hooks that need to inspect or mutate messages (e.g., for
/// summarization or context injection). The underlying `Vec<Message>` is
/// borrowed mutably for the duration of the hook call.
pub struct ConversationView<'a> {
    messages: &'a mut Vec<Message>,
}

impl<'a> ConversationView<'a> {
    /// Create a new view over a message vector.
    pub fn new(messages: &'a mut Vec<Message>) -> Self {
        Self { messages }
    }

    /// Number of messages in the conversation.
    pub fn len(&self) -> usize {
        self.messages.len()
    }

    /// Whether the conversation is empty.
    pub fn is_empty(&self) -> bool {
        self.messages.is_empty()
    }

    /// Read-only access to all messages.
    pub fn messages(&self) -> &[Message] {
        self.messages
    }

    /// The last `n` messages (or all if fewer exist).
    pub fn last_n(&self, n: usize) -> &[Message] {
        let start = self.messages.len().saturating_sub(n);
        &self.messages[start..]
    }

    /// Append a message to the end of the conversation.
    pub fn push(&mut self, msg: Message) {
        self.messages.push(msg);
    }

    /// Insert a message at a specific position.
    pub fn insert(&mut self, index: usize, msg: Message) {
        self.messages.insert(index, msg);
    }

    /// Remove and return messages in the given range.
    pub fn drain(&mut self, range: std::ops::Range<usize>) -> Vec<Message> {
        self.messages.drain(range).collect()
    }

    /// Replace a range of messages with a single summary message.
    ///
    /// Useful for compressing old context to save tokens.
    pub fn summarize_range(&mut self, range: std::ops::Range<usize>, summary: Message) {
        let start = range.start;
        self.messages.drain(range);
        self.messages.insert(start, summary);
    }

    /// Estimate total tokens across all messages.
    ///
    /// Uses byte-length heuristic via [`estimate_tokens_from_size`].
    pub fn estimated_tokens(&self) -> u64 {
        self.messages
            .iter()
            .map(|m| {
                let bytes = match &m.content {
                    brainwires_core::MessageContent::Text(t) => t.len() as u64,
                    brainwires_core::MessageContent::Blocks(blocks) => {
                        blocks.iter().map(|b| format!("{:?}", b).len() as u64).sum()
                    }
                };
                estimate_tokens_from_size(bytes) as u64
            })
            .sum()
    }

    /// Get the text of the most recent assistant message, if any.
    pub fn last_assistant_text(&self) -> Option<&str> {
        self.messages
            .iter()
            .rev()
            .find(|m| m.role == Role::Assistant)
            .and_then(|m| m.text())
    }
}

// ── Main trait ───────────────────────────────────────────────────────────────

/// Granular lifecycle hooks for controlling an agent's execution loop.
///
/// All methods have default no-op implementations — consumers only override
/// the hooks they need. Hooks receive an [`IterationContext`] for read-only
/// state inspection, and some receive a [`ConversationView`] for history
/// manipulation.
///
/// # Relationship to `brainwires_core::lifecycle`
///
/// The core lifecycle system is **observational** — hooks receive events and
/// can cancel operations, but cannot modify conversation state or delegate work.
///
/// This trait is **controlling** — hooks can skip iterations, override tool
/// results, delegate to sub-agents, and rewrite conversation history. It is
/// wired into the agent loop itself, not the event dispatch system.
#[async_trait]
pub trait AgentLifecycleHooks: Send + Sync {
    // ── Iteration-level hooks ────────────────────────────────────────

    /// Called at the top of each iteration, before budget checks and the
    /// provider call.
    ///
    /// Return [`IterationDecision::Skip`] to skip the provider call
    /// (e.g., if you injected messages yourself).
    /// Return [`IterationDecision::Abort`] to stop the loop with failure.
    async fn on_before_iteration(
        &self,
        _ctx: &IterationContext<'_>,
        _conversation: &mut ConversationView<'_>,
    ) -> IterationDecision {
        IterationDecision::Continue
    }

    /// Called after all tools have been executed (or after a completion
    /// check), before the next iteration starts.
    ///
    /// Good for context management, summarization, and metrics.
    async fn on_after_iteration(
        &self,
        _ctx: &IterationContext<'_>,
        _conversation: &mut ConversationView<'_>,
    ) {
    }

    // ── Provider call hooks ──────────────────────────────────────────

    /// Called immediately before the provider is called.
    ///
    /// Can modify conversation (e.g., inject context summaries).
    async fn on_before_provider_call(
        &self,
        _ctx: &IterationContext<'_>,
        _conversation: &mut ConversationView<'_>,
    ) {
    }

    /// Called immediately after the provider returns a response.
    async fn on_after_provider_call(
        &self,
        _ctx: &IterationContext<'_>,
        _response: &brainwires_core::ChatResponse,
    ) {
    }

    // ── Tool execution hooks ─────────────────────────────────────────

    /// Called before each tool is executed.
    ///
    /// Return [`ToolDecision::Delegate`] to spawn a sub-agent instead of
    /// executing the tool directly. Return [`ToolDecision::Override`] to
    /// skip execution and inject a custom result.
    async fn on_before_tool_execution(
        &self,
        _ctx: &IterationContext<'_>,
        _tool_use: &ToolUse,
    ) -> ToolDecision {
        ToolDecision::Execute
    }

    /// Called after each tool execution completes.
    ///
    /// Can inspect the result and modify conversation (e.g., spawn a
    /// sub-agent to analyze complex results).
    async fn on_after_tool_execution(
        &self,
        _ctx: &IterationContext<'_>,
        _tool_use: &ToolUse,
        _result: &ToolResult,
        _conversation: &mut ConversationView<'_>,
    ) {
    }

    // ── Completion hooks ─────────────────────────────────────────────

    /// Called when the agent signals completion, before validation runs.
    ///
    /// Return `false` to reject the completion attempt and force the
    /// agent to continue iterating.
    async fn on_before_completion(
        &self,
        _ctx: &IterationContext<'_>,
        _completion_text: &str,
    ) -> bool {
        true
    }

    /// Called after a successful completion (validation passed).
    async fn on_after_completion(
        &self,
        _ctx: &IterationContext<'_>,
        _result: &crate::task_agent::TaskAgentResult,
    ) {
    }

    // ── Context management hooks ─────────────────────────────────────

    /// Called when the estimated conversation token count exceeds the
    /// configured budget.
    ///
    /// The consumer can summarize, compress, or evict messages to stay
    /// within the budget. Only called when
    /// [`TaskAgentConfig::context_budget_tokens`] is set.
    async fn on_context_pressure(
        &self,
        _ctx: &IterationContext<'_>,
        _conversation: &mut ConversationView<'_>,
        _estimated_tokens: u64,
        _budget_tokens: u64,
    ) {
    }

    // ── Delegation hooks ─────────────────────────────────────────────

    /// Called when a [`ToolDecision::Delegate`] needs to be fulfilled.
    ///
    /// The default returns an error — consumers must override this if
    /// they use delegation. See [`DefaultDelegationHandler`] for a
    /// ready-made implementation wrapping [`AgentPool`].
    async fn execute_delegation(&self, _request: &DelegationRequest) -> Result<DelegationResult> {
        Err(anyhow::anyhow!(
            "Delegation unsupported by this hook provider. \
             Override execute_delegation() or use DefaultDelegationHandler."
        ))
    }
}

// ── Default delegation handler ───────────────────────────────────────────────

/// Convenience handler that delegates work via an [`AgentPool`].
///
/// Consumers can compose this into their own [`AgentLifecycleHooks`]
/// implementation to get delegation support without writing pool logic:
///
/// ```rust,ignore
/// struct MyHooks {
///     delegator: DefaultDelegationHandler,
/// }
///
/// #[async_trait::async_trait]
/// impl AgentLifecycleHooks for MyHooks {
///     async fn execute_delegation(&self, req: &DelegationRequest) -> Result<DelegationResult> {
///         self.delegator.delegate(req).await
///     }
/// }
/// ```
pub struct DefaultDelegationHandler {
    pool: Arc<AgentPool>,
}

impl DefaultDelegationHandler {
    /// Create a handler backed by the given agent pool.
    pub fn new(pool: Arc<AgentPool>) -> Self {
        Self { pool }
    }

    /// Execute a delegation request by spawning a sub-agent in the pool.
    pub async fn delegate(&self, request: &DelegationRequest) -> Result<DelegationResult> {
        let task = brainwires_core::Task::new(
            uuid::Uuid::new_v4().to_string(),
            request.task_description.clone(),
        );
        let agent_id = self.pool.spawn_agent(task, request.config.clone()).await?;

        if request.blocking {
            let result = self.pool.await_completion(&agent_id).await?;
            Ok(DelegationResult {
                agent_id: result.agent_id,
                success: result.success,
                output: result.summary,
                iterations_used: result.iterations,
                tokens_used: result.total_tokens_used,
            })
        } else {
            Ok(DelegationResult {
                agent_id,
                success: true,
                output: "Delegation started (non-blocking)".to_string(),
                iterations_used: 0,
                tokens_used: 0,
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use brainwires_core::{Message, MessageContent, Role};

    #[test]
    fn test_conversation_view_len_and_empty() {
        let mut msgs = vec![];
        let view = ConversationView::new(&mut msgs);
        assert!(view.is_empty());
        assert_eq!(view.len(), 0);
    }

    #[test]
    fn test_conversation_view_push_and_messages() {
        let mut msgs = vec![];
        let mut view = ConversationView::new(&mut msgs);
        view.push(Message::user("hello"));
        view.push(Message::user("world"));
        assert_eq!(view.len(), 2);
        assert_eq!(view.messages()[0].text(), Some("hello"));
    }

    #[test]
    fn test_conversation_view_last_n() {
        let mut msgs = vec![Message::user("a"), Message::user("b"), Message::user("c")];
        let view = ConversationView::new(&mut msgs);
        assert_eq!(view.last_n(2).len(), 2);
        assert_eq!(view.last_n(2)[0].text(), Some("b"));
        assert_eq!(view.last_n(100).len(), 3); // clamps to total
    }

    #[test]
    fn test_conversation_view_drain() {
        let mut msgs = vec![
            Message::user("keep"),
            Message::user("remove1"),
            Message::user("remove2"),
            Message::user("keep2"),
        ];
        let mut view = ConversationView::new(&mut msgs);
        let removed = view.drain(1..3);
        assert_eq!(removed.len(), 2);
        assert_eq!(view.len(), 2);
        assert_eq!(view.messages()[0].text(), Some("keep"));
        assert_eq!(view.messages()[1].text(), Some("keep2"));
    }

    #[test]
    fn test_conversation_view_summarize_range() {
        let mut msgs = vec![
            Message::user("first"),
            Message::user("old1"),
            Message::user("old2"),
            Message::user("old3"),
            Message::user("last"),
        ];
        let mut view = ConversationView::new(&mut msgs);
        view.summarize_range(1..4, Message::user("[summary of 3 messages]"));
        assert_eq!(view.len(), 3);
        assert_eq!(view.messages()[0].text(), Some("first"));
        assert_eq!(view.messages()[1].text(), Some("[summary of 3 messages]"));
        assert_eq!(view.messages()[2].text(), Some("last"));
    }

    #[test]
    fn test_conversation_view_estimated_tokens() {
        let mut msgs = vec![Message::user("hello world, this is a test message")];
        let view = ConversationView::new(&mut msgs);
        let tokens = view.estimated_tokens();
        assert!(tokens > 0);
    }

    #[test]
    fn test_conversation_view_last_assistant_text() {
        let mut msgs = vec![
            Message::user("question"),
            Message {
                role: Role::Assistant,
                content: MessageContent::Text("answer".to_string()),
                name: None,
                metadata: None,
            },
            Message::user("follow-up"),
        ];
        let view = ConversationView::new(&mut msgs);
        assert_eq!(view.last_assistant_text(), Some("answer"));
    }

    #[test]
    fn test_conversation_view_insert() {
        let mut msgs = vec![Message::user("first"), Message::user("last")];
        let mut view = ConversationView::new(&mut msgs);
        view.insert(1, Message::user("middle"));
        assert_eq!(view.len(), 3);
        assert_eq!(view.messages()[1].text(), Some("middle"));
    }

    #[test]
    fn test_iteration_decision_variants() {
        let _continue = IterationDecision::Continue;
        let _skip = IterationDecision::Skip;
        let _abort = IterationDecision::Abort("reason".to_string());
    }

    #[test]
    fn test_tool_decision_variants() {
        let _execute = ToolDecision::Execute;
        let _override =
            ToolDecision::Override(ToolResult::success("id".to_string(), "ok".to_string()));
        let _delegate = ToolDecision::Delegate(Box::new(DelegationRequest::default()));
    }

    #[test]
    fn test_delegation_request_default() {
        let req = DelegationRequest::default();
        assert!(req.task_description.is_empty());
        assert!(req.config.is_none());
        assert!(req.seed_messages.is_empty());
        assert!(req.blocking);
    }
}