Skip to main content

rig_memory/migration/
testing.rs

1//! A synthetic format before [`FORMAT`] and the chain from it, for tests
2//! of migrations through a store's own reads and writes. Rig never wrote
3//! this format, and [`SHIPPED`] has no step from it.
4//!
5//! ```
6//! use rig_memory::migration::testing;
7//! use rig_memory::{MemoryStore, Session};
8//!
9//! let store = MemoryStore::default();
10//! testing::write_fixture(&store)?;
11//! assert!(Session::read(&store).is_err());
12//! testing::CHAIN.migrate(&store, &|_, _| Ok(()))?;
13//! Session::read(&store)?;
14//! # Ok::<(), rig_memory::SessionError>(())
15//! ```
16
17use rig_core::conversation::{Aside, Author, Conversation};
18use rig_core::message::{ImageMediaType, Message, UserContent};
19use serde_json::json;
20
21use super::*;
22use crate::{BLOBS, Files};
23
24/// The synthetic format: as [`FORMAT`], with its blobs under
25/// [`SYNTHETIC_BLOBS`] in place of [`BLOBS`].
26pub const SYNTHETIC: &str = "rig-session/synthetic";
27
28/// Where a session in [`SYNTHETIC`] keeps its blobs.
29pub const SYNTHETIC_BLOBS: &str = "synthetic-blobs/";
30
31/// The key of a saved value of a plugin that is not installed, which the
32/// fixture holds.
33pub const UNINSTALLED: &str = "uninstalled.state";
34
35/// The file of a plugin that is not installed, which the fixture holds.
36pub const UNINSTALLED_FILE: &str = "plugins/uninstalled/data";
37
38/// The chain from [`SYNTHETIC`].
39pub const CHAIN: Chain = Chain {
40    steps: &[Step {
41        from: SYNTHETIC,
42        to: FORMAT,
43        apply: from_synthetic,
44        #[cfg(test)]
45        fixture,
46    }],
47};
48
49/// The step from [`SYNTHETIC`]: each log's header names [`FORMAT`], and
50/// each blob moves to [`BLOBS`].
51fn from_synthetic(session: &mut Snapshot) -> Result<(), SessionError> {
52    rename_format(session, FORMAT)?;
53    move_files(session, SYNTHETIC_BLOBS, BLOBS);
54    Ok(())
55}
56
57/// Writes a session in [`SYNTHETIC`] into the empty `store`, generated by
58/// today's writer and turned back by the inverse of the step: two
59/// messages, one with an image; an aside and a saved value of a plugin
60/// that is not installed; the session's listing; and a plugin's file.
61///
62/// # Errors
63///
64/// When writing or the store fails.
65pub fn write_fixture(store: &dyn Store) -> Result<(), SessionError> {
66    let snapshot = Snapshot::read(&fixture()?)?;
67    snapshot.write_to(store)
68}
69
70fn fixture() -> Result<MemoryStore, SessionError> {
71    let store = MemoryStore::default();
72    let mut conversation = Conversation::from_messages([
73        Message::User {
74            content: vec![
75                UserContent::text("a synthetic session"),
76                UserContent::image_base64("cG5n", Some(ImageMediaType::PNG), None),
77            ],
78        },
79        Message::assistant("seen"),
80    ]);
81    conversation.add_aside(Aside::new(
82        "uninstalled.aside",
83        Author::plugin("uninstalled"),
84        json!({"kept": [1, "as is"]}),
85    ));
86    crate::save(&store, "agent", &mut conversation)?;
87    let mut session = Session::read(&store)?;
88    session
89        .saved
90        .insert(UNINSTALLED.to_owned(), json!({"kept": [false, "as is"]}));
91    session.save(&store)?;
92    store.write_file(META, br#"{"title":"synthetic"}"#)?;
93    store.write_file(UNINSTALLED_FILE, b"opaque\0\xff bytes")?;
94    let mut snapshot = Snapshot::read(&store)?;
95    rename_format(&mut snapshot, SYNTHETIC)?;
96    move_files(&mut snapshot, BLOBS, SYNTHETIC_BLOBS);
97    let synthetic = MemoryStore::default();
98    snapshot.write_to(&synthetic)?;
99    Ok(synthetic)
100}
101
102/// Sets the format each log's header, its first line, names to `to`.
103fn rename_format(session: &mut Snapshot, to: &str) -> Result<(), SessionError> {
104    for bytes in session.logs.values_mut() {
105        let Some((first, rest)) =
106            memchr::memchr(b'\n', bytes).and_then(|at| bytes.split_at_checked(at))
107        else {
108            continue;
109        };
110        let mut header: Value = serde_json::from_slice(first).map_err(invalid_json)?;
111        if let Some(header) = header.as_object_mut() {
112            header.insert("format".to_owned(), to.into());
113        }
114        let mut renamed = serde_json::to_vec(&header).map_err(invalid_json)?;
115        renamed.extend_from_slice(rest);
116        *bytes = renamed;
117    }
118    Ok(())
119}
120
121/// Moves each file under `from` to `to`.
122fn move_files(session: &mut Snapshot, from: &str, to: &str) {
123    let moved: Vec<String> = session
124        .files
125        .keys()
126        .filter(|name| name.starts_with(from))
127        .cloned()
128        .collect();
129    for name in moved {
130        if let (Some(bytes), Some(rest)) = (session.files.remove(&name), name.strip_prefix(from)) {
131            session.files.insert(format!("{to}{rest}"), bytes);
132        }
133    }
134}