behest 0.3.1

A Rust-native cloud agent runtime with typed tools, pluggable memory, queues, and observability.
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
//! Context pipeline for runtime.
//!
//! Wraps the existing `ContextFactory` with runtime-specific adapters
//! for session history, compaction-aware message filtering, and
//! token-budget management.

use std::sync::Arc;

use uuid::Uuid;

use crate::context::{ContextAdapter, ContextFactory, ContextInput, ContextOutput};
use crate::provider::{ChatRequest, ContentPart, Message, ModelName, ToolSpec};
use crate::store::MessageRecord;
use crate::token::estimate_records_tokens;

use super::error::RuntimeResult;

/// Runtime context pipeline that composes context from multiple sources.
///
/// The pipeline:
/// 1. Loads session history from the store
/// 2. Applies compaction filter (reorder post-compaction messages)
/// 3. Invokes registered context adapters (system prompt, RAG, etc.)
/// 4. Applies token-budget trimming as a safety net
/// 5. Produces a final [`ChatRequest`] or [`ContextOutput`]
pub struct ContextPipeline {
    factory: ContextFactory,
    max_history_messages: usize,
    max_history_tokens: usize,
    enable_compaction_filter: bool,
}

impl ContextPipeline {
    /// Creates a new context pipeline with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self {
            factory: ContextFactory::new(),
            max_history_messages: 50,
            max_history_tokens: 64_000,
            enable_compaction_filter: true,
        }
    }

    /// Creates a context pipeline with an existing context factory.
    #[must_use]
    pub fn with_factory(factory: ContextFactory) -> Self {
        Self {
            factory,
            max_history_messages: 50,
            max_history_tokens: 64_000,
            enable_compaction_filter: true,
        }
    }

    /// Sets the maximum number of history messages to include as a fallback limit.
    ///
    /// This limit applies before token-based trimming. Default: 50.
    #[must_use]
    pub fn with_max_history(mut self, max: usize) -> Self {
        self.max_history_messages = max;
        self
    }

    /// Sets the maximum token budget for historical messages before trimming.
    ///
    /// Older messages are dropped first when the budget is exceeded.
    /// Default: 64,000 tokens.
    #[must_use]
    pub fn with_max_history_tokens(mut self, max: usize) -> Self {
        self.max_history_tokens = max;
        self
    }

    /// Enables or disables the compaction message filter.
    ///
    /// When enabled, compacted message pairs are replaced with a synthetic
    /// checkpoint system message. Enabled by default.
    #[must_use]
    pub fn with_compaction_filter(mut self, enable: bool) -> Self {
        self.enable_compaction_filter = enable;
        self
    }

    /// Registers a context adapter.
    pub fn register<A>(&mut self, adapter: A)
    where
        A: ContextAdapter + 'static,
    {
        self.factory.register(adapter);
    }

    /// Registers an already shared context adapter.
    pub fn register_arc(&mut self, adapter: Arc<dyn ContextAdapter>) {
        self.factory.register_arc(adapter);
    }

    /// Returns adapter names in registration order.
    pub fn adapter_names(&self) -> impl Iterator<Item = &str> {
        self.factory.adapter_names()
    }

    /// Builds a [`ChatRequest`] from session history, registered adapters,
    /// and the current user message.
    ///
    /// The pipeline loads session messages, applies compaction filtering,
    /// token-budget trimming, runs registered adapters, and assembles the
    /// final request with optional tool specs.
    ///
    /// # Errors
    ///
    /// Returns `RuntimeError` on adapter failure or storage errors.
    pub async fn build(
        &self,
        store: &super::store::RuntimeStore,
        session_id: Uuid,
        model: ModelName,
        user_message: Option<&str>,
        tools: Option<&[ToolSpec]>,
    ) -> RuntimeResult<ChatRequest> {
        let input = ContextInput {
            user_message: user_message.map(str::to_owned),
            session_id: Some(session_id.to_string()),
            metadata: serde_json::Value::Null,
        };

        let mut output = self.factory.build(&input).await.map_err(|e| {
            super::error::RuntimeError::Context(crate::error::ContextError::AdapterFailed {
                adapter: "pipeline".to_owned(),
                message: e.to_string(),
            })
        })?;

        let records = store
            .sessions()
            .list_messages(&session_id)
            .await
            .map_err(super::error::RuntimeError::Storage)?;

        let records = if self.enable_compaction_filter {
            apply_compaction_filter(records)
        } else {
            records
        };

        let records = trim_by_tokens(records, self.max_history_tokens);

        let history: Vec<Message> = records
            .into_iter()
            .filter_map(super::store::record_to_message)
            .collect();

        output.extend(history);

        if let Some(text) = user_message {
            output.extend([Message::user_text(text)]);
        }

        let request = match tools {
            Some(specs) => output.into_request_with_tools(model, specs),
            None => output.into_request(model),
        };

        Ok(request)
    }

    /// Builds context output without creating a chat request.
    ///
    /// Equivalent to [`build`](Self::build) but returns the raw
    /// [`ContextOutput`] instead of wrapping it in a [`ChatRequest`].
    /// Useful when the caller needs access to the composed message list
    /// without binding to a specific model or tools.
    ///
    /// # Errors
    ///
    /// Returns `RuntimeError` on adapter failure or storage errors.
    pub async fn build_context(
        &self,
        store: &super::store::RuntimeStore,
        session_id: Uuid,
        user_message: Option<&str>,
    ) -> RuntimeResult<ContextOutput> {
        let input = ContextInput {
            user_message: user_message.map(str::to_owned),
            session_id: Some(session_id.to_string()),
            metadata: serde_json::Value::Null,
        };

        let mut output = self.factory.build(&input).await.map_err(|e| {
            super::error::RuntimeError::Context(crate::error::ContextError::AdapterFailed {
                adapter: "pipeline".to_owned(),
                message: e.to_string(),
            })
        })?;

        let records = store
            .sessions()
            .list_messages(&session_id)
            .await
            .map_err(super::error::RuntimeError::Storage)?;

        let records = if self.enable_compaction_filter {
            apply_compaction_filter(records)
        } else {
            records
        };

        let records = trim_by_tokens(records, self.max_history_tokens);

        let history: Vec<Message> = records
            .into_iter()
            .filter_map(super::store::record_to_message)
            .collect();

        output.extend(history);

        if let Some(text) = user_message {
            output.extend([Message::user_text(text)]);
        }

        Ok(output)
    }
}

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

/// Applies the compaction message filter to session history.
///
/// Finds the latest valid compaction pair (compaction marker + summary) and
/// reorders messages so that the compacted head is replaced by a synthetic
/// checkpoint system message while the retained tail and post-compaction
/// messages remain visible.
///
/// Returns the filtered message list with the compacted head removed.
///
/// Ported from OpenCode V1's `filterCompacted()`.
fn apply_compaction_filter(records: Vec<MessageRecord>) -> Vec<MessageRecord> {
    if records.is_empty() {
        return records;
    }

    // Walk backwards to find the latest completed compaction pair:
    //   summary_assistant (is_summary=true) immediately after
    //   compaction_user (is_compaction=true)
    let mut summary_idx: Option<usize> = None;
    let mut compaction_idx: Option<usize> = None;

    for i in (0..records.len()).rev() {
        let rec = &records[i];
        if rec.is_summary && summary_idx.is_none() {
            summary_idx = Some(i);
        } else if rec.is_compaction && compaction_idx.is_none() && summary_idx.is_some() {
            // Found the compaction user that precedes this summary
            compaction_idx = Some(i);
            break;
        } else if !rec.is_summary && !rec.is_compaction && summary_idx.is_some() {
            // We found a summary but the preceding message is NOT a compaction user
            // Reset — this is not a valid compaction pair
            summary_idx = None;
        }
    }

    let (Some(c_idx), Some(s_idx)) = (compaction_idx, summary_idx) else {
        return records;
    };

    let tail_start_id = records[c_idx]
        .compaction_meta
        .as_ref()
        .and_then(|m| m.tail_start_id);

    let Some(tail_start) = tail_start_id else {
        return records;
    };

    // Find the index of tail_start_id
    let tail_idx = records.iter().position(|r| r.id == tail_start);

    // Split records into three groups:
    //   before: [0..tail_idx) — the compacted head (EXCLUDED)
    //   tail:   [tail_idx..compact_end) — the retained tail
    //   after:  [compact_end..) — post-compaction messages
    //
    // compact_end = first non-compaction message after summary_idx
    let compact_end = records
        .iter()
        .skip(s_idx + 1)
        .position(|r| !r.is_compaction && !r.is_summary)
        .map_or(records.len(), |p| s_idx + 1 + p);

    // Build result:
    //   1. Compaction checkpoint as a synthetic system message
    //   2. Retained tail (from tail_idx to between compaction and summary)
    //   3. Post-compaction messages (after compact_end)

    let mut result = Vec::with_capacity(records.len());

    // Phase 1: Synthetic compaction checkpoint
    // Build a system message from the compaction pair
    if let Some(summary_meta) = &records[s_idx].compaction_meta {
        if let Some(summary_text) = &summary_meta.summary_text {
            let checkpoint = MessageRecord {
                id: Uuid::now_v7(),
                session_id: records[c_idx].session_id,
                role: crate::store::MessageRole::System,
                content: vec![ContentPart::text(format!(
                    "<conversation-checkpoint>\n<summary>\n{summary_text}\n</summary>\n</conversation-checkpoint>"
                ))],
                tool_calls: Vec::new(),
                tool_call_id: None,
                tool_name: None,
                usage: None,
                created_at: records[s_idx].created_at,
                is_compaction: false,
                is_summary: false,
                compaction_meta: None,
            };
            result.push(checkpoint);
        }
    }

    // Phase 2: Retained tail (messages between tail_start and compaction_user)
    if let Some(ti) = tail_idx {
        let tail_end = c_idx.min(records.len());
        for rec in records.iter().skip(ti).take(tail_end.saturating_sub(ti)) {
            if !rec.is_compaction && !rec.is_summary {
                result.push(rec.clone());
            }
        }
    }

    // Phase 3: Post-compaction messages (everything after the summary)
    for rec in records.iter().skip(compact_end) {
        result.push(rec.clone());
    }

    result
}

/// Trims message history to stay within a token budget.
///
/// Walks from the end of the list forward, accumulating token estimates,
/// and drops the oldest messages when the budget is exceeded.
///
/// Returns the trimmed message list ordered from oldest to newest.
fn trim_by_tokens(records: Vec<MessageRecord>, max_tokens: usize) -> Vec<MessageRecord> {
    if records.is_empty() {
        return records;
    }

    let total = estimate_records_tokens(&records);
    if total <= max_tokens {
        return records;
    }

    // Note: first system message preservation is handled implicitly —
    // since we walk backwards, the earliest messages (including system)
    // are the first ones dropped when budget is exceeded.

    // Walk backwards, keeping messages until budget exceeded
    let mut kept = Vec::new();
    let mut tokens = 0usize;

    for rec in records.into_iter().rev() {
        let rec_tokens = crate::token::estimate_record_tokens(&rec);
        if tokens + rec_tokens > max_tokens && !kept.is_empty() {
            // Don't add this message — it would exceed budget
            // But keep the system message if we haven't included it yet
            break;
        }
        tokens += rec_tokens;
        kept.push(rec);
    }

    kept.reverse();

    // Re-prepend the system message if it was dropped
    // If we dropped the system message due to extreme length, that's acceptable

    kept
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::context::StaticAdapter;
    use crate::provider::ContentPart;
    use crate::runtime::memory::MemoryRunStore;
    use crate::store::memory::{MemoryExecutionStore, MemorySessionStore};
    use crate::store::{CompactionMeta, MessageRole, Session};

    fn make_store() -> super::super::store::RuntimeStore {
        let sessions = MemorySessionStore::new();
        let executions = MemoryExecutionStore::new();
        let runs = MemoryRunStore::new();
        super::super::store::RuntimeStore::new(
            Box::new(sessions),
            Box::new(executions),
            Box::new(runs),
        )
    }

    fn make_record_for(session_id: Uuid, role: MessageRole, text: &str) -> MessageRecord {
        MessageRecord::new(session_id, role, vec![ContentPart::text(text)])
    }

    #[tokio::test]
    async fn pipeline_should_compose_system_and_history() {
        let store = make_store();

        let session = Session::new("Test", ModelName::new("gpt-4"));
        store
            .sessions()
            .create_session(session.clone())
            .await
            .unwrap();

        let user_msg = make_record_for(session.id, MessageRole::User, "Hello");
        store.sessions().append_message(user_msg).await.unwrap();

        let mut pipeline = ContextPipeline::new();
        pipeline.register(StaticAdapter::system("You are helpful."));

        let request = pipeline
            .build(
                &store,
                session.id,
                ModelName::new("gpt-4"),
                Some("How are you?"),
                None,
            )
            .await
            .unwrap();

        assert_eq!(request.messages.len(), 3);
        assert!(matches!(request.messages[0], Message::System { .. }));
        assert!(matches!(request.messages[1], Message::User { .. }));
        assert!(matches!(request.messages[2], Message::User { .. }));
    }

    #[tokio::test]
    async fn pipeline_should_apply_token_trim() {
        let store = make_store();

        let session = Session::new("Test", ModelName::new("gpt-4"));
        store
            .sessions()
            .create_session(session.clone())
            .await
            .unwrap();

        for i in 0..10 {
            let msg = make_record_for(session.id, MessageRole::User, &format!("Message {i}"));
            store.sessions().append_message(msg).await.unwrap();
        }

        // Very restrictive token budget — should only keep a few messages
        let pipeline = ContextPipeline::new().with_max_history_tokens(50);

        let request = pipeline
            .build(&store, session.id, ModelName::new("gpt-4"), None, None)
            .await
            .unwrap();

        // Should have fewer messages than the original 10
        assert!(request.messages.len() < 10);
    }

    #[tokio::test]
    async fn pipeline_should_filter_compacted_head() {
        let store = make_store();

        let session = Session::new("Test", ModelName::new("gpt-4"));
        store
            .sessions()
            .create_session(session.clone())
            .await
            .unwrap();

        let sid = session.id;

        // Create messages: m1, m2 (head), m3, m4 (tail)
        let m1 = make_record_for(sid, MessageRole::User, "m1");
        let m2 = make_record_for(sid, MessageRole::Assistant, "m2");
        let m3 = make_record_for(sid, MessageRole::User, "m3");
        let m4 = make_record_for(sid, MessageRole::Assistant, "m4");

        let m3_id = m3.id;

        store.sessions().append_message(m1).await.unwrap();
        store.sessions().append_message(m2).await.unwrap();
        store.sessions().append_message(m3).await.unwrap();
        store.sessions().append_message(m4).await.unwrap();

        // Append compaction pair
        let compaction_user = MessageRecord {
            id: Uuid::now_v7(),
            session_id: sid,
            role: MessageRole::User,
            content: vec![ContentPart::text("[compaction]")],
            tool_calls: Vec::new(),
            tool_call_id: None,
            tool_name: None,
            usage: None,
            created_at: chrono::Utc::now(),
            is_compaction: true,
            is_summary: false,
            compaction_meta: Some(CompactionMeta::new(m3_id)),
        };
        store
            .sessions()
            .append_message(compaction_user)
            .await
            .unwrap();

        let summary_msg = MessageRecord {
            id: Uuid::now_v7(),
            session_id: sid,
            role: MessageRole::Assistant,
            content: vec![ContentPart::text("Summary of m1-m2")],
            tool_calls: Vec::new(),
            tool_call_id: None,
            tool_name: None,
            usage: None,
            created_at: chrono::Utc::now(),
            is_compaction: false,
            is_summary: true,
            compaction_meta: Some(
                CompactionMeta::new(m3_id).with_summary("Summary of m1-m2".to_owned()),
            ),
        };
        store.sessions().append_message(summary_msg).await.unwrap();

        // Append post-compaction message
        let m5 = make_record_for(sid, MessageRole::User, "m5");
        store.sessions().append_message(m5).await.unwrap();

        let pipeline = ContextPipeline::new();

        let request = pipeline
            .build(&store, sid, ModelName::new("gpt-4"), None, None)
            .await
            .unwrap();

        // Should NOT include m1, m2 (compacted head)
        // Should include: checkpoint system message, m3, m4, m5
        let has_m1 = request.messages.iter().any(|m| {
            matches!(m, Message::User { content } if content.iter().any(|p| matches!(p, ContentPart::Text { text } if text == "m1")))
        });
        let has_m3 = request.messages.iter().any(|m| {
            matches!(m, Message::User { content } if content.iter().any(|p| matches!(p, ContentPart::Text { text } if text == "m3")))
        });
        let has_checkpoint = request.messages.iter().any(|m| {
            matches!(m, Message::System { content } if content.iter().any(|p| matches!(p, ContentPart::Text { text } if text.contains("conversation-checkpoint"))))
        });

        assert!(!has_m1, "compacted head should be excluded");
        assert!(has_m3, "retained tail should be included");
        assert!(has_checkpoint, "compaction checkpoint should be present");
    }

    #[tokio::test]
    async fn pipeline_no_compaction_returns_all() {
        let store = make_store();

        let session = Session::new("Test", ModelName::new("gpt-4"));
        store
            .sessions()
            .create_session(session.clone())
            .await
            .unwrap();

        for i in 0..5 {
            let msg = make_record_for(session.id, MessageRole::User, &format!("msg{i}"));
            store.sessions().append_message(msg).await.unwrap();
        }

        let pipeline = ContextPipeline::new().with_compaction_filter(true);

        let request = pipeline
            .build(&store, session.id, ModelName::new("gpt-4"), None, None)
            .await
            .unwrap();

        assert_eq!(request.messages.len(), 5);
    }

    #[test]
    fn trim_by_tokens_preserves_tail() {
        let sid = Uuid::now_v7();
        let records: Vec<MessageRecord> = (0..20)
            .map(|i| make_record_for(sid, MessageRole::User, &format!("msg{i:03}")))
            .collect();

        let trimmed = trim_by_tokens(records, 100);

        // Should have fewer records, but not empty
        assert!(!trimmed.is_empty());
        assert!(trimmed.len() < 20);
    }

    #[test]
    fn trim_by_tokens_all_when_under_budget() {
        let sid = Uuid::now_v7();
        let records = vec![
            make_record_for(sid, MessageRole::User, "hi"),
            make_record_for(sid, MessageRole::Assistant, "hello"),
        ];

        let trimmed = trim_by_tokens(records.clone(), 1000);
        assert_eq!(trimmed.len(), 2);
    }

    #[test]
    fn filter_compacted_no_compaction_returns_unchanged() {
        let sid = Uuid::now_v7();
        let records = vec![
            make_record_for(sid, MessageRole::User, "a"),
            make_record_for(sid, MessageRole::Assistant, "b"),
        ];

        let filtered = apply_compaction_filter(records.clone());
        assert_eq!(filtered.len(), 2);
    }
}