bob-runtime 0.3.2

Runtime orchestration layer for Bob 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
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
//! # Agent Loop
//!
//! High-level orchestration loop that unifies slash command routing, tape
//! recording, and LLM pipeline execution.
//!
//! ## Architecture
//!
//! ```text
//! ┌─────────────┐       ┌────────────┐       ┌─────────────────┐
//! │  Channel     │ ───→  │ AgentLoop  │ ───→  │  AgentRuntime   │
//! │ (transport)  │ ←───  │ (routing)  │ ←───  │  (LLM pipeline) │
//! └─────────────┘       └────────────┘       └─────────────────┘
//!//!                           ├─→ Router (slash commands)
//!                           ├─→ TapeStorePort (recording)
//!                           └─→ ToolPort (tool listing for /tools)
//! ```
//!
//! The `AgentLoop` wraps an `AgentRuntime` and adds:
//!
//! 1. **Slash command routing** — deterministic commands bypass the LLM
//! 2. **Tape recording** — conversation history is persisted to the tape
//! 3. **System prompt override** — load custom system prompts from workspace files

use std::sync::Arc;

use bob_core::{
    error::AgentError,
    ports::{EventSink, SessionStore, ToolPort},
    tape::{TapeEntryKind, TapeSearchResult},
    types::{AgentRequest, AgentRunResult, RequestContext, TokenUsage},
};

// Re-export for convenience.
pub use crate::router::help_text;
use crate::{
    AgentRuntime,
    router::{self, RouteResult, SlashCommand},
};

/// Output from the agent loop after processing a single input.
#[derive(Debug)]
pub enum AgentLoopOutput {
    /// A response from the LLM pipeline (normal conversation).
    Response(AgentRunResult),
    /// A deterministic command response (no LLM involved).
    CommandOutput(String),
    /// The user requested to quit the session.
    Quit,
}

/// High-level agent orchestration loop.
///
/// Wraps an `AgentRuntime` with slash command routing, tape recording,
/// and optional system prompt overrides.
///
/// ## Construction
///
/// ```rust,ignore
/// let agent_loop = AgentLoop::new(runtime, tools)
///     .with_tape(tape_store)
///     .with_system_prompt("You are a helpful assistant.".to_string());
/// ```
pub struct AgentLoop {
    runtime: Arc<dyn AgentRuntime>,
    tools: Arc<dyn ToolPort>,
    store: Option<Arc<dyn SessionStore>>,
    tape: Option<Arc<dyn bob_core::ports::TapeStorePort>>,
    events: Option<Arc<dyn EventSink>>,
    system_prompt_override: Option<String>,
}

impl std::fmt::Debug for AgentLoop {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AgentLoop")
            .field("has_store", &self.store.is_some())
            .field("has_tape", &self.tape.is_some())
            .field("has_system_prompt_override", &self.system_prompt_override.is_some())
            .finish_non_exhaustive()
    }
}

impl AgentLoop {
    /// Create a new agent loop wrapping the given runtime and tool port.
    #[must_use]
    pub fn new(runtime: Arc<dyn AgentRuntime>, tools: Arc<dyn ToolPort>) -> Self {
        Self { runtime, tools, store: None, tape: None, events: None, system_prompt_override: None }
    }

    /// Attach a session store for slash commands that inspect persisted state.
    #[must_use]
    pub fn with_store(mut self, store: Arc<dyn SessionStore>) -> Self {
        self.store = Some(store);
        self
    }

    /// Attach a tape store for persistent conversation recording.
    #[must_use]
    pub fn with_tape(mut self, tape: Arc<dyn bob_core::ports::TapeStorePort>) -> Self {
        self.tape = Some(tape);
        self
    }

    /// Attach an event sink for observability.
    #[must_use]
    pub fn with_events(mut self, events: Arc<dyn EventSink>) -> Self {
        self.events = Some(events);
        self
    }

    /// Set a system prompt that overrides the default runtime prompt.
    ///
    /// This is typically loaded from a workspace file (e.g. `.agent/system-prompt.md`)
    /// at the composition root. The content is passed as a pre-loaded string to
    /// keep the runtime free from filesystem dependencies.
    #[must_use]
    pub fn with_system_prompt(mut self, prompt: String) -> Self {
        self.system_prompt_override = Some(prompt);
        self
    }

    /// Process a single user input and return the appropriate output.
    ///
    /// Slash commands are handled deterministically. Natural language input
    /// is forwarded to the LLM pipeline.
    pub async fn handle_input(
        &self,
        input: &str,
        session_id: &str,
    ) -> Result<AgentLoopOutput, AgentError> {
        self.handle_input_with_context(input, session_id, RequestContext::default()).await
    }

    /// Process a single user input using an explicit request context.
    pub async fn handle_input_with_context(
        &self,
        input: &str,
        session_id: &str,
        context: RequestContext,
    ) -> Result<AgentLoopOutput, AgentError> {
        let sid = session_id.to_string();

        match router::route(input) {
            RouteResult::SlashCommand(cmd) => self.execute_command(cmd, &sid).await,
            RouteResult::NaturalLanguage(text) => {
                if let Some(ref tape) = self.tape {
                    let _ = tape
                        .append(
                            &sid,
                            TapeEntryKind::Message {
                                role: bob_core::types::Role::User,
                                content: text.clone(),
                            },
                        )
                        .await;
                }
                self.execute_llm(&text, &sid, context).await
            }
        }
    }

    /// Execute a deterministic slash command.
    async fn execute_command(
        &self,
        cmd: SlashCommand,
        session_id: &String,
    ) -> Result<AgentLoopOutput, AgentError> {
        match cmd {
            SlashCommand::Help => Ok(AgentLoopOutput::CommandOutput(help_text())),

            SlashCommand::Tools => {
                let tools = self.tools.list_tools().await?;
                let mut out = String::from("Registered tools:\n");
                for tool in &tools {
                    out.push_str(&format!("  - {}: {}\n", tool.id, tool.description));
                }
                if tools.is_empty() {
                    out.push_str("  (none)\n");
                }
                Ok(AgentLoopOutput::CommandOutput(out))
            }

            SlashCommand::ToolDescribe { name } => {
                let tools = self.tools.list_tools().await?;
                let found = tools.iter().find(|t| t.id == name);
                let out = match found {
                    Some(tool) => {
                        format!(
                            "Tool: {}\nDescription: {}\nSource: {:?}\nSchema:\n{}",
                            tool.id,
                            tool.description,
                            tool.source,
                            serde_json::to_string_pretty(&tool.input_schema).unwrap_or_default()
                        )
                    }
                    None => {
                        format!("Tool '{}' not found. Use /tools to list available tools.", name)
                    }
                };
                Ok(AgentLoopOutput::CommandOutput(out))
            }

            SlashCommand::TapeSearch { query } => {
                let out = if let Some(ref tape) = self.tape {
                    let results = tape.search(session_id, &query).await?;
                    format_search_results(&results)
                } else {
                    "Tape not configured.".to_string()
                };
                Ok(AgentLoopOutput::CommandOutput(out))
            }

            SlashCommand::TapeInfo => {
                let out = if let Some(ref tape) = self.tape {
                    let entries = tape.all_entries(session_id).await?;
                    let anchors = tape.anchors(session_id).await?;
                    format!("Tape: {} entries, {} anchors", entries.len(), anchors.len())
                } else {
                    "Tape not configured.".to_string()
                };
                Ok(AgentLoopOutput::CommandOutput(out))
            }

            SlashCommand::Anchors => {
                let out = if let Some(ref tape) = self.tape {
                    let anchors = tape.anchors(session_id).await?;
                    if anchors.is_empty() {
                        "No anchors in tape.".to_string()
                    } else {
                        let mut buf = String::from("Anchors:\n");
                        for a in &anchors {
                            if let TapeEntryKind::Anchor { ref name, .. } = a.kind {
                                buf.push_str(&format!("  [{}] {}\n", a.id, name));
                            }
                        }
                        buf
                    }
                } else {
                    "Tape not configured.".to_string()
                };
                Ok(AgentLoopOutput::CommandOutput(out))
            }

            SlashCommand::Handoff { name } => {
                let handoff_name = name.unwrap_or_else(|| "manual".to_string());
                let reset_applied = if let Some(ref store) = self.store {
                    let retained_usage = store
                        .load(session_id)
                        .await?
                        .map_or_else(TokenUsage::default, |state| state.total_usage);
                    store
                        .save(
                            session_id,
                            &bob_core::types::SessionState {
                                messages: Vec::new(),
                                total_usage: retained_usage,
                                ..Default::default()
                            },
                        )
                        .await?;
                    true
                } else {
                    false
                };

                if let Some(ref tape) = self.tape {
                    let all = tape.all_entries(session_id).await?;
                    let _ = tape
                        .append(
                            session_id,
                            TapeEntryKind::Handoff {
                                name: handoff_name.clone(),
                                entries_before: all.len() as u64,
                                summary: None,
                            },
                        )
                        .await;
                    let message = if reset_applied {
                        format!("Handoff '{}' created. Context window reset.", handoff_name)
                    } else {
                        format!(
                            "Handoff '{}' recorded, but session store is not configured so context was not reset.",
                            handoff_name
                        )
                    };
                    Ok(AgentLoopOutput::CommandOutput(message))
                } else if reset_applied {
                    Ok(AgentLoopOutput::CommandOutput(format!(
                        "Context window reset for handoff '{}'. Tape not configured.",
                        handoff_name
                    )))
                } else {
                    Ok(AgentLoopOutput::CommandOutput(
                        "Handoff requires a session store or tape configuration.".to_string(),
                    ))
                }
            }

            SlashCommand::Usage => {
                let out = if let Some(ref store) = self.store {
                    let session = store.load(session_id).await?;
                    format_usage_summary(session.as_ref().map(|state| &state.total_usage))
                } else {
                    "Session store not configured.".to_string()
                };
                Ok(AgentLoopOutput::CommandOutput(out))
            }

            SlashCommand::Quit => Ok(AgentLoopOutput::Quit),

            SlashCommand::Shell { command } => {
                // Shell execution is a convenience fallback.
                // In production, this should be gated by approval policies.
                Ok(AgentLoopOutput::CommandOutput(format!(
                    "Shell execution not yet implemented: {}",
                    command
                )))
            }
        }
    }

    /// Forward natural language input to the LLM pipeline.
    async fn execute_llm(
        &self,
        text: &str,
        session_id: &String,
        mut context: RequestContext,
    ) -> Result<AgentLoopOutput, AgentError> {
        if let Some(ref prompt) = self.system_prompt_override {
            context.system_prompt = Some(prompt.clone());
        }

        let req = AgentRequest {
            input: text.to_string(),
            session_id: session_id.clone(),
            model: None,
            context,
            cancel_token: None,
            output_schema: None,
            max_output_retries: 0,
        };

        let result = self.runtime.run(req).await?;

        // Record assistant response to tape.
        if let Some(ref tape) = self.tape {
            let AgentRunResult::Finished(ref resp) = result;
            let _ = tape
                .append(
                    session_id,
                    TapeEntryKind::Message {
                        role: bob_core::types::Role::Assistant,
                        content: resp.content.clone(),
                    },
                )
                .await;
        }

        Ok(AgentLoopOutput::Response(result))
    }
}

/// Format tape search results into a human-readable string.
fn format_search_results(results: &[TapeSearchResult]) -> String {
    if results.is_empty() {
        return "No results found.".to_string();
    }
    let mut buf = format!("{} result(s):\n", results.len());
    for r in results {
        buf.push_str(&format!("  [{}] {}\n", r.entry.id, r.snippet));
    }
    buf
}

fn format_usage_summary(usage: Option<&TokenUsage>) -> String {
    let usage = usage.cloned().unwrap_or_default();
    format!(
        "Session usage:\n  Prompt tokens: {}\n  Completion tokens: {}\n  Total tokens: {}",
        usage.prompt_tokens,
        usage.completion_tokens,
        usage.total(),
    )
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};

    use bob_core::{
        error::{AgentError, StoreError, ToolError},
        ports::{TapeStorePort, ToolPort},
        tape::{TapeEntry, TapeEntryKind, TapeSearchResult},
        types::{
            AgentEventStream, AgentResponse, FinishReason, RuntimeHealth, SessionId, SessionState,
            ToolCall, ToolDescriptor, ToolResult,
        },
    };

    use super::*;

    struct StubRuntime;

    #[async_trait::async_trait]
    impl AgentRuntime for StubRuntime {
        async fn run(&self, _req: AgentRequest) -> Result<AgentRunResult, AgentError> {
            Ok(AgentRunResult::Finished(AgentResponse {
                content: "stub".to_string(),
                tool_transcript: Vec::new(),
                usage: TokenUsage::default(),
                finish_reason: FinishReason::Stop,
            }))
        }

        async fn run_stream(&self, _req: AgentRequest) -> Result<AgentEventStream, AgentError> {
            Err(AgentError::Config("unused in test".to_string()))
        }

        async fn health(&self) -> RuntimeHealth {
            RuntimeHealth {
                status: bob_core::types::HealthStatus::Healthy,
                llm_ready: true,
                mcp_pool_ready: true,
            }
        }
    }

    struct StubTools;

    #[async_trait::async_trait]
    impl ToolPort for StubTools {
        async fn list_tools(&self) -> Result<Vec<ToolDescriptor>, ToolError> {
            Ok(Vec::new())
        }

        async fn call_tool(&self, call: ToolCall) -> Result<ToolResult, ToolError> {
            Err(ToolError::NotFound { name: call.name })
        }
    }

    struct StaticSessionStore {
        state: SessionState,
    }

    #[async_trait::async_trait]
    impl SessionStore for StaticSessionStore {
        async fn load(&self, _id: &SessionId) -> Result<Option<SessionState>, StoreError> {
            Ok(Some(self.state.clone()))
        }

        async fn save(&self, _id: &SessionId, _state: &SessionState) -> Result<(), StoreError> {
            Ok(())
        }
    }

    #[derive(Default)]
    struct MemorySessionStore {
        state: Mutex<Option<SessionState>>,
    }

    #[async_trait::async_trait]
    impl SessionStore for MemorySessionStore {
        async fn load(&self, _id: &SessionId) -> Result<Option<SessionState>, StoreError> {
            Ok(self.state.lock().unwrap_or_else(|poisoned| poisoned.into_inner()).clone())
        }

        async fn save(&self, _id: &SessionId, state: &SessionState) -> Result<(), StoreError> {
            *self.state.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) =
                Some(state.clone());
            Ok(())
        }
    }

    #[derive(Default)]
    struct MemoryTapeStore {
        entries: Mutex<Vec<TapeEntry>>,
    }

    #[async_trait::async_trait]
    impl TapeStorePort for MemoryTapeStore {
        async fn append(
            &self,
            _session_id: &SessionId,
            kind: TapeEntryKind,
        ) -> Result<TapeEntry, StoreError> {
            let mut entries = self.entries.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
            let entry = TapeEntry { id: entries.len() as u64 + 1, kind, timestamp_ms: 0 };
            entries.push(entry.clone());
            Ok(entry)
        }

        async fn entries_since_last_handoff(
            &self,
            _session_id: &SessionId,
        ) -> Result<Vec<TapeEntry>, StoreError> {
            Ok(Vec::new())
        }

        async fn search(
            &self,
            _session_id: &SessionId,
            _query: &str,
        ) -> Result<Vec<TapeSearchResult>, StoreError> {
            Ok(Vec::new())
        }

        async fn all_entries(&self, _session_id: &SessionId) -> Result<Vec<TapeEntry>, StoreError> {
            Ok(self.entries.lock().unwrap_or_else(|poisoned| poisoned.into_inner()).clone())
        }

        async fn anchors(&self, _session_id: &SessionId) -> Result<Vec<TapeEntry>, StoreError> {
            Ok(Vec::new())
        }
    }

    #[tokio::test]
    async fn usage_command_reads_total_usage_from_store() {
        let store = Arc::new(StaticSessionStore {
            state: SessionState {
                messages: Vec::new(),
                total_usage: TokenUsage { prompt_tokens: 12, completion_tokens: 8 },
                ..Default::default()
            },
        });
        let loop_ = AgentLoop::new(Arc::new(StubRuntime), Arc::new(StubTools)).with_store(store);

        let output = loop_.handle_input("/usage", "session-1").await;

        match output {
            Ok(AgentLoopOutput::CommandOutput(body)) => {
                assert!(body.contains("Prompt tokens: 12"));
                assert!(body.contains("Completion tokens: 8"));
                assert!(body.contains("Total tokens: 20"));
            }
            Ok(other) => panic!("expected usage command output, got {other:?}"),
            Err(err) => panic!("usage command failed: {err}"),
        }
    }

    #[tokio::test]
    async fn slash_commands_do_not_append_user_messages_to_tape() {
        let store = Arc::new(StaticSessionStore {
            state: SessionState {
                messages: Vec::new(),
                total_usage: TokenUsage { prompt_tokens: 12, completion_tokens: 8 },
                ..Default::default()
            },
        });
        let tape = Arc::new(MemoryTapeStore::default());
        let loop_ = AgentLoop::new(Arc::new(StubRuntime), Arc::new(StubTools))
            .with_store(store)
            .with_tape(tape.clone());

        let output = loop_.handle_input("/usage", "session-1").await;

        match output {
            Ok(AgentLoopOutput::CommandOutput(body)) => {
                assert!(body.contains("Total tokens: 20"));
            }
            Ok(other) => panic!("expected usage command output, got {other:?}"),
            Err(err) => panic!("usage command failed: {err}"),
        }

        let entries = tape.all_entries(&"session-1".to_string()).await;
        let entries = match entries {
            Ok(entries) => entries,
            Err(err) => panic!("failed to read tape entries: {err}"),
        };
        assert!(entries.is_empty(), "slash commands should not be recorded as tape messages");
    }

    #[tokio::test]
    async fn natural_language_turns_still_append_user_and_assistant_messages_to_tape() {
        let tape = Arc::new(MemoryTapeStore::default());
        let loop_ =
            AgentLoop::new(Arc::new(StubRuntime), Arc::new(StubTools)).with_tape(tape.clone());

        let output = loop_.handle_input("hello world", "session-1").await;

        match output {
            Ok(AgentLoopOutput::Response(AgentRunResult::Finished(resp))) => {
                assert_eq!(resp.content, "stub");
            }
            Ok(other) => panic!("expected LLM response output, got {other:?}"),
            Err(err) => panic!("natural language turn failed: {err}"),
        }

        let entries = tape.all_entries(&"session-1".to_string()).await;
        let entries = match entries {
            Ok(entries) => entries,
            Err(err) => panic!("failed to read tape entries: {err}"),
        };
        assert_eq!(
            entries.len(),
            2,
            "natural language turns should record both user and assistant"
        );
        assert!(matches!(
            entries.first().map(|entry| &entry.kind),
            Some(TapeEntryKind::Message { role: bob_core::types::Role::User, content })
                if content == "hello world"
        ));
        assert!(matches!(
            entries.get(1).map(|entry| &entry.kind),
            Some(TapeEntryKind::Message { role: bob_core::types::Role::Assistant, content })
                if content == "stub"
        ));
    }

    #[tokio::test]
    async fn handoff_resets_session_messages_but_keeps_usage() {
        let store = Arc::new(MemorySessionStore {
            state: Mutex::new(Some(SessionState {
                messages: vec![
                    bob_core::types::Message::text(bob_core::types::Role::User, "before"),
                    bob_core::types::Message::text(bob_core::types::Role::Assistant, "answer"),
                ],
                total_usage: TokenUsage { prompt_tokens: 21, completion_tokens: 13 },
                ..Default::default()
            })),
        });
        let tape = Arc::new(MemoryTapeStore::default());
        let loop_ = AgentLoop::new(Arc::new(StubRuntime), Arc::new(StubTools))
            .with_store(store.clone())
            .with_tape(tape.clone());

        let output = loop_.handle_input("/handoff phase-2", "session-1").await;

        match output {
            Ok(AgentLoopOutput::CommandOutput(body)) => {
                assert!(body.contains("Context window reset"));
            }
            Ok(other) => panic!("expected handoff command output, got {other:?}"),
            Err(err) => panic!("handoff command failed: {err}"),
        }

        let saved = store.load(&"session-1".to_string()).await;
        let saved = match saved {
            Ok(Some(state)) => state,
            other => panic!("expected saved session state, got {other:?}"),
        };
        assert!(saved.messages.is_empty(), "handoff should clear session messages");
        assert_eq!(saved.total_usage.total(), 34, "handoff should preserve cumulative usage");

        let entries = tape.all_entries(&"session-1".to_string()).await;
        let entries = match entries {
            Ok(entries) => entries,
            Err(err) => panic!("failed to read tape entries: {err}"),
        };
        assert_eq!(entries.len(), 1, "handoff should not leave a slash-command message on tape");
        assert!(
            entries.iter().any(|entry| matches!(
                entry.kind,
                TapeEntryKind::Handoff { ref name, .. } if name == "phase-2"
            )),
            "handoff should be recorded to the tape",
        );
    }
}