rig-agentcore 0.47.4

Run a Rig agent on AWS Bedrock AgentCore Runtime: the HTTP protocol contract (/invocations, /ping), sessions, streaming and traces AgentCore Observability and Evaluations read.
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
//! Where an app keeps sessions, and how much of one a request sends.
//!
//! By default sessions live in memory, at most `max_sessions` of them, and
//! each keeps only the newest messages its window allows, so memory stays
//! bounded. [`AgentCoreApp::history_store`](crate::AgentCoreApp::history_store)
//! keeps them in any [`rig_memory`] store instead (`MemoryStore`, `DirStore`
//! on disk, `rig-sqlite`, `rig-postgres`). The store then keeps each
//! session's whole conversation, so it survives a restart and can be shared
//! by several apps, while the window still decides what a request sends.
//! Whatever the session id, its log has a safe name (see [`Persistent`]).

use std::collections::{HashMap, VecDeque};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, LazyLock, Mutex, MutexGuard, PoisonError};

use rig_agent::Agent;
use rig_core::conversation::pairing::safe_start;
use rig_core::message::{Message, UserContent};
use rig_core::wasm_compat::{WasmBoxedFuture, WasmCompatSend, WasmCompatSync};
use rig_memory::recent::{Recent, view};
use rig_memory::{
    AsyncStore, SUMMARIZATION_SYSTEM_PROMPT, SessionError, SummaryState, apply_summary, load_async,
    save_async, serialize_conversation,
};
use sha2::{Digest, Sha256};
use tracing::{Instrument, Span};

/// The longest session id whose own characters name its log. Longer or
/// other ids are hashed.
const MAX_LOG_ID: usize = 128;
/// How often a save that lost a race with another writer is tried in all.
const SAVE_ATTEMPTS: u32 = 10;

pub(crate) type HistoryError = Box<dyn std::error::Error + Send + Sync>;

/// A store of sessions' conversations by session id.
pub(crate) trait HistoryStore: WasmCompatSend + WasmCompatSync + 'static {
    /// The messages of `session` the store holds, oldest first; none for a
    /// new one. The caller applies the window.
    fn load<'a>(
        &'a self,
        session: &'a str,
    ) -> WasmBoxedFuture<'a, Result<Vec<Arc<Message>>, HistoryError>>;

    /// Add a finished run's `messages` to the end of `session`. `window` is
    /// what a request sends; a store that keeps only that much may trim to it.
    fn append<'a>(
        &'a self,
        session: &'a str,
        messages: Vec<Message>,
        window: &'a dyn Recent,
    ) -> WasmBoxedFuture<'a, Result<(), HistoryError>>;

    /// Replace the messages `read` (the first of `session`, as the caller
    /// loaded them) with one summary message. `None`, changing nothing, when
    /// the session no longer starts with `read`. Otherwise the session's
    /// messages after the change.
    fn replace_head<'a>(
        &'a self,
        session: &'a str,
        read: &'a [Arc<Message>],
        summary: &'a SummaryState,
    ) -> WasmBoxedFuture<'a, Result<Option<Vec<Arc<Message>>>, HistoryError>>;
}

/// Whether `session` can name its log as it is: 1 to 128 ASCII letters,
/// digits, `-` or `_`, so a file store can use it as a path part.
fn names_its_log(session: &str) -> bool {
    !session.is_empty()
        && session.len() <= MAX_LOG_ID
        && session
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'_')
}

/// Sessions in memory, bounded in count. Each keeps only what the window
/// sends, trimmed when a run is saved.
// ponytail: FIFO eviction by start time; switch to LRU if long-lived sessions get evicted while active.
pub(crate) struct InMemory(Mutex<Bounded>);

struct Bounded {
    by_session: HashMap<String, Vec<Arc<Message>>>,
    started: VecDeque<String>,
    max: usize,
}

impl InMemory {
    pub(crate) fn new(max: usize) -> Self {
        Self(Mutex::new(Bounded {
            by_session: HashMap::new(),
            started: VecDeque::new(),
            max: max.max(1),
        }))
    }

    fn lock(&self) -> MutexGuard<'_, Bounded> {
        self.0.lock().unwrap_or_else(PoisonError::into_inner)
    }
}

impl HistoryStore for InMemory {
    fn load<'a>(
        &'a self,
        session: &'a str,
    ) -> WasmBoxedFuture<'a, Result<Vec<Arc<Message>>, HistoryError>> {
        let messages = self
            .lock()
            .by_session
            .get(session)
            .cloned()
            .unwrap_or_default();
        Box::pin(async move { Ok(messages) })
    }

    fn append<'a>(
        &'a self,
        session: &'a str,
        messages: Vec<Message>,
        window: &'a dyn Recent,
    ) -> WasmBoxedFuture<'a, Result<(), HistoryError>> {
        let mut bounded = self.lock();
        if !bounded.by_session.contains_key(session) {
            bounded.started.push_back(session.to_owned());
            while bounded.started.len() > bounded.max {
                if let Some(oldest) = bounded.started.pop_front() {
                    bounded.by_session.remove(&oldest);
                }
            }
        }
        let conversation = bounded.by_session.entry(session.to_owned()).or_default();
        conversation.extend(messages.into_iter().map(Arc::new));
        // Keep only what a request would send (never splitting a tool call from
        // its result), so memory stays bounded by the window.
        let kept = view(conversation, window).len();
        conversation.drain(..conversation.len() - kept);
        Box::pin(async { Ok(()) })
    }

    fn replace_head<'a>(
        &'a self,
        session: &'a str,
        read: &'a [Arc<Message>],
        summary: &'a SummaryState,
    ) -> WasmBoxedFuture<'a, Result<Option<Vec<Arc<Message>>>, HistoryError>> {
        let mut bounded = self.lock();
        let replaced = bounded
            .by_session
            .get_mut(session)
            .and_then(|conversation| {
                let same = conversation
                    .get(..read.len())?
                    .iter()
                    .zip(read)
                    .all(|(now, read)| Arc::ptr_eq(now, read));
                if !same || read.is_empty() {
                    return None;
                }
                let text = summary.message()?;
                conversation.drain(..read.len());
                conversation.insert(0, Arc::new(Message::user(text)));
                Some(conversation.clone())
            });
        Box::pin(async { Ok(replaced) })
    }
}

/// Sessions in a [`rig_memory`] store, one session log per session id.
pub(crate) struct Persistent<S>(pub(crate) S);

impl<S> Persistent<S> {
    /// The log of `session`, kept apart from other sessions in the store. A
    /// session id that names its log as it is keeps its characters. Any
    /// other (AgentCore sends ids of 33 to 256 characters, of anything) is
    /// hashed, so no id can escape a file store. The two prefixes keep a
    /// kept id and a hash from ever sharing a name.
    fn log(session: &str) -> String {
        if names_its_log(session) {
            format!("agentcore-s-{session}")
        } else {
            let digest = Sha256::digest(session.as_bytes());
            let hex: String = digest.iter().map(|byte| format!("{byte:02x}")).collect();
            format!("agentcore-h-{hex}")
        }
    }
}

impl<S> HistoryStore for Persistent<S>
where
    S: AsyncStore + WasmCompatSend + WasmCompatSync + 'static,
{
    fn load<'a>(
        &'a self,
        session: &'a str,
    ) -> WasmBoxedFuture<'a, Result<Vec<Arc<Message>>, HistoryError>> {
        Box::pin(async move {
            let conversation = load_async(&self.0, &Self::log(session)).await?;
            Ok(conversation.messages().to_vec())
        })
    }

    fn append<'a>(
        &'a self,
        session: &'a str,
        messages: Vec<Message>,
        _window: &'a dyn Recent,
    ) -> WasmBoxedFuture<'a, Result<(), HistoryError>> {
        Box::pin(async move {
            let log = Self::log(session);
            for attempt in 1..=SAVE_ATTEMPTS {
                // Another writer may have saved since this one read; the store
                // then refuses the save whole, so reread and add on top.
                let mut conversation = load_async(&self.0, &log).await?;
                conversation.extend(messages.iter().cloned());
                match save_async(&self.0, &log, &mut conversation).await {
                    Ok(()) => return Ok(()),
                    Err(SessionError::Changed { .. }) if attempt < SAVE_ATTEMPTS => {
                        tokio::time::sleep(backoff(attempt)).await;
                    }
                    Err(error) => return Err(error.into()),
                }
            }
            Err(format!("the log {log} changed under {SAVE_ATTEMPTS} saves").into())
        })
    }

    fn replace_head<'a>(
        &'a self,
        session: &'a str,
        read: &'a [Arc<Message>],
        summary: &'a SummaryState,
    ) -> WasmBoxedFuture<'a, Result<Option<Vec<Arc<Message>>>, HistoryError>> {
        Box::pin(async move {
            let log = Self::log(session);
            let mut conversation = load_async(&self.0, &log).await?;
            // The store's own messages, so rig-memory's splice (which needs the
            // same messages it was given) can apply; a session that no longer
            // starts with `read` is left alone.
            let replaces: Vec<Arc<Message>> = {
                let now = conversation.messages();
                let same = now
                    .get(..read.len())
                    .is_some_and(|now| now.iter().zip(read).all(|(now, read)| **now == **read));
                match now.get(..read.len()) {
                    Some(head) if same && !read.is_empty() => head.to_vec(),
                    _ => return Ok(None),
                }
            };
            if apply_summary(&mut conversation, &replaces, summary).is_none() {
                return Ok(None);
            }
            save_async(&self.0, &log, &mut conversation).await?;
            Ok(Some(conversation.messages().to_vec()))
        })
    }
}

/// How long a save waits before its `attempt`-th retry: a few milliseconds,
/// growing, with a jitter from the clock so writers that collided do not
/// collide again in step.
fn backoff(attempt: u32) -> std::time::Duration {
    let jitter = u64::from(
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |elapsed| elapsed.subsec_nanos())
            % 8,
    );
    std::time::Duration::from_millis(u64::from(attempt) * 2 + jitter)
}

/// The summarizing window of [`AgentCoreApp::summarize_history`](crate::AgentCoreApp::summarize_history).
#[derive(Clone)]
pub(crate) struct Summarizing {
    pub(crate) summarizer: Agent,
    pub(crate) keep_recent: usize,
    pub(crate) trigger_messages: usize,
}

/// Why a session's history was not summarized. Only logged: the caller then
/// sends the full history.
#[derive(Debug, thiserror::Error)]
enum SummarizeError {
    #[error("the summarizer failed: {0}")]
    Run(String),
    #[error("the summarizer returned no text")]
    Empty,
    #[error("the summary could not be stored: {0}")]
    Store(String),
    #[error("the conversation changed while it was summarized")]
    Changed,
}

impl SummarizeError {
    /// The kind of failure, which is all a log line may say without `record_content`.
    fn kind(&self) -> &'static str {
        match self {
            Self::Run(_) => "run_failed",
            Self::Empty => "empty_reply",
            Self::Store(_) => "store_error",
            Self::Changed => "changed",
        }
    }
}

/// A probe summary whose text marks where the summary text falls in its frame.
const PROBE: &str = "\u{1}";

/// The text every summary message opens with: rig-memory's frame, before the summary.
static OPENING: LazyLock<String> = LazyLock::new(|| {
    SummaryState {
        summary: PROBE.to_owned(),
        ..SummaryState::default()
    }
    .message()
    .unwrap_or_default()
    .split(PROBE)
    .next()
    .unwrap_or_default()
    .to_owned()
});

/// Whether `message` is a summary this module wrote (or rig-memory's splice did).
pub(crate) fn is_summary(message: &Message) -> bool {
    let Message::User { content } = message else {
        return false;
    };
    matches!(
        content.first(),
        Some(UserContent::Text(text)) if text.text.starts_with(OPENING.as_str())
    )
}

/// What a session keeps when a summarizer is set: the window's messages, and
/// at least the newest `floor` messages, so a summarizer that keeps failing
/// cannot grow a session without bound.
pub(crate) struct Retained<'a> {
    pub(crate) window: &'a dyn Recent,
    pub(crate) floor: usize,
}

impl Recent for Retained<'_> {
    fn start(&self, messages: &[Arc<Message>]) -> usize {
        self.window
            .start(messages)
            .min(messages.len().saturating_sub(self.floor))
    }
}

/// The request text for a summary of `read`, the messages it replaces.
fn summary_prompt(read: &[Arc<Message>]) -> String {
    format!(
        "{SUMMARIZATION_SYSTEM_PROMPT}\n\n<conversation>\n{}\n</conversation>\n\n\
         Summarize the conversation above in plain prose and short lists: the user's goals, \
         decisions made, facts and names later turns depend on, and tasks still open. If it \
         begins with an earlier summary, carry its facts forward.",
        serialize_conversation(read)
    )
}

/// Replace the older part of `messages` (all but the newest `keep_recent`, cut
/// so no tool result is separated from its call) with one summary in `store`,
/// and return the session's messages after it. `None` when nothing is older
/// than the newest messages or the summary failed; the failure is logged and
/// the history is left as it was.
///
/// Each attempt runs in an `agentcore.summarize` span, under the invocation
/// span, with its outcome: `summarized`, `skipped_changed`, `failed`, or
/// `skipped_nothing_older`. The prompt and the summary are recorded only when
/// `record_content` is set.
pub(crate) async fn summarize(
    store: &dyn HistoryStore,
    summarizing: &Summarizing,
    session: &str,
    messages: &[Arc<Message>],
    record_content: bool,
) -> Option<Vec<Arc<Message>>> {
    let cut = safe_start(
        messages,
        messages.len().saturating_sub(summarizing.keep_recent),
    );
    let kept = messages.len().saturating_sub(cut);
    let span = tracing::info_span!(
        "agentcore.summarize",
        agentcore.session_id = %session,
        agentcore.summary.messages_summarized = cut,
        agentcore.summary.messages_kept = kept,
        agentcore.summary.outcome = tracing::field::Empty,
        gen_ai.usage.input_tokens = tracing::field::Empty,
        gen_ai.usage.output_tokens = tracing::field::Empty,
        agentcore.summary.prompt = tracing::field::Empty,
        agentcore.summary.text = tracing::field::Empty,
    );
    let Some(read) = messages.get(..cut).filter(|read| !read.is_empty()) else {
        span.record("agentcore.summary.outcome", "skipped_nothing_older");
        return None;
    };
    let attempt = try_summarize(store, summarizing, session, read, record_content, &span)
        .instrument(span.clone())
        .await;
    match attempt {
        Ok(summarized) => {
            span.record("agentcore.summary.outcome", "summarized");
            tracing::info!(
                parent: &span,
                messages_summarized = cut,
                messages_kept = kept,
                "summarized the conversation"
            );
            Some(summarized)
        }
        Err(SummarizeError::Changed) => {
            span.record("agentcore.summary.outcome", "skipped_changed");
            tracing::info!(
                parent: &span,
                "the conversation changed while it was summarized; keeping its history"
            );
            None
        }
        Err(error) => {
            span.record("agentcore.summary.outcome", "failed");
            let shown = crate::logged(record_content, error.kind(), &error);
            tracing::warn!(parent: &span, error = %shown, "could not summarize the conversation; keeping its full history");
            None
        }
    }
}

async fn try_summarize(
    store: &dyn HistoryStore,
    summarizing: &Summarizing,
    session: &str,
    read: &[Arc<Message>],
    record_content: bool,
    span: &Span,
) -> Result<Vec<Arc<Message>>, SummarizeError> {
    let prompt = summary_prompt(read);
    if record_content {
        span.record("agentcore.summary.prompt", prompt.as_str());
    }
    let response = summarizing
        .summarizer
        .prompt(prompt)
        .run()
        .await
        .map_err(|failure| SummarizeError::Run(failure.to_string()))?;
    let usage = response.usage();
    if let Some(input) = usage.input_tokens {
        span.record("gen_ai.usage.input_tokens", input);
    }
    if let Some(output) = usage.output_tokens {
        span.record("gen_ai.usage.output_tokens", output);
    }
    let text = response.output();
    let text = text.trim();
    if text.is_empty() {
        return Err(SummarizeError::Empty);
    }
    if record_content {
        span.record("agentcore.summary.text", text);
    }
    let state = SummaryState {
        summary: text.to_owned(),
        ..SummaryState::default()
    };
    store
        .replace_head(session, read, &state)
        .await
        .map_err(|failure| SummarizeError::Store(failure.to_string()))?
        .ok_or(SummarizeError::Changed)
}

/// A session id for a call that came without one (outside AgentCore).
pub(crate) fn local_id() -> String {
    static NEXT: AtomicU64 = AtomicU64::new(0);
    format!(
        "local-{}-{}",
        std::process::id(),
        NEXT.fetch_add(1, Ordering::Relaxed)
    )
}

#[cfg(test)]
mod tests {
    use rig_memory::HeuristicTokenCounter;
    use rig_memory::recent::{LastMessages, TokenBudget};

    use super::*;

    #[tokio::test]
    async fn a_session_keeps_only_its_newest_history_within_the_window() {
        // Every message costs the same (400 bytes), so the budget holds exactly
        // ten: runs 195 to 199, each a prompt and a reply.
        let filler = "x".repeat(392);
        let cost =
            HeuristicTokenCounter::default().count(&Message::user(format!("run 000 {filler}")));
        let window = TokenBudget::new(cost * 10 + cost / 2);
        let store = InMemory::new(1);
        for run in 0..200 {
            store
                .append(
                    "session",
                    vec![
                        Message::user(format!("run {run:03} {filler}")),
                        Message::assistant(format!("reply {run:03} {filler}")),
                    ],
                    &window,
                )
                .await
                .unwrap();
        }
        let kept = store.load("session").await.unwrap();
        assert_eq!(kept.len(), 10);
        assert!(
            kept.first()
                .is_some_and(|m| format!("{m:?}").contains("run 195"))
        );
        assert!(
            kept.last()
                .is_some_and(|m| format!("{m:?}").contains("reply 199"))
        );
    }

    #[tokio::test]
    async fn an_empty_window_keeps_no_history() {
        let store = InMemory::new(1);
        store
            .append("session", vec![Message::user("hello")], &LastMessages(0))
            .await
            .unwrap();
        assert!(store.load("session").await.unwrap().is_empty());
    }

    #[test]
    fn every_session_id_names_a_safe_log() {
        assert_eq!(
            Persistent::<()>::log("019a2b3c-4d5e-7f00-8a9b-0c1d2e3f4a5b"),
            "agentcore-s-019a2b3c-4d5e-7f00-8a9b-0c1d2e3f4a5b"
        );
        // Escapes, separators and a 256-character id all become a fixed-length hash.
        for odd in ["../etc", "a/b", "a.b", "a b", &"x".repeat(256)] {
            let log = Persistent::<()>::log(odd);
            assert!(log.starts_with("agentcore-h-"), "{odd:?}");
            assert_eq!(log.len(), "agentcore-h-".len() + 64, "{odd:?}");
            assert!(
                log["agentcore-h-".len()..]
                    .bytes()
                    .all(|byte| byte.is_ascii_hexdigit()),
                "{odd:?}"
            );
        }
        // A hashed id can never equal a kept one, even one that looks like a hash.
        assert_eq!(Persistent::<()>::log("h-kept"), "agentcore-s-h-kept");
        assert_ne!(
            Persistent::<()>::log("a/b"),
            Persistent::<()>::log("a_b"),
            "distinct ids keep distinct logs"
        );
    }
}