Skip to main content

laser_wire/
memory.rs

1use serde::{Deserialize, Serialize};
2
3/// A memory record as it rides the memory topic: the on-log audit of one scope's
4/// changes. This is the shape the SDK's memory facade writes and the shape a
5/// deployment folds into the versioned key-value read view, so both sides agree
6/// on the bytes without the reader importing the SDK. Ids are the text form of
7/// the SDK's memory id, kinds the snake-case kind word, so the encoding is
8/// byte-identical to the SDK's own record. Runtime-free and portable, like the
9/// rest of the wire contract.
10#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
11pub enum MemoryRecord {
12    /// A remembered item: its id, kind word, and body.
13    Item {
14        id: String,
15        kind: String,
16        body: Vec<u8>,
17        #[serde(default, skip_serializing_if = "Option::is_none")]
18        origin: Option<crate::graph::SourceRef>,
19        #[serde(default, skip_serializing_if = "Option::is_none")]
20        producer: Option<crate::graph::ProducerInfo>,
21    },
22    /// A tombstone removing an item from recall. With `conversation`, it
23    /// removes the item only when the item was remembered in that conversation.
24    Forget {
25        target: String,
26        #[serde(default, skip_serializing_if = "Option::is_none")]
27        conversation: Option<String>,
28    },
29    /// A feedback signal reweighting an item's recall rank. With
30    /// `conversation`, it counts only for an item remembered in that
31    /// conversation.
32    Feedback {
33        target: String,
34        weight: f32,
35        #[serde(default, skip_serializing_if = "Option::is_none")]
36        conversation: Option<String>,
37    },
38}
39
40#[cfg(all(test, feature = "cbor"))]
41mod tests {
42    use super::*;
43    use crate::framing::{decode_named, encode_named};
44
45    #[test]
46    fn given_each_variant_when_round_tripped_then_should_preserve_fields() {
47        for record in [
48            MemoryRecord::Item {
49                id: "01KWM3K3XEP3NP5TN850J17YBP".to_owned(),
50                kind: "fact".to_owned(),
51                body: b"auth is slow".to_vec(),
52                origin: None,
53                producer: None,
54            },
55            MemoryRecord::Forget {
56                target: "01KWM3K3XEP3NP5TN850J17YBP".to_owned(),
57                conversation: Some("01KWM3K3XEP3NP5TN850J17YBQ".to_owned()),
58            },
59            MemoryRecord::Feedback {
60                target: "01KWM3K3XEP3NP5TN850J17YBP".to_owned(),
61                weight: 1.5,
62                conversation: None,
63            },
64        ] {
65            let bytes = encode_named(&record).expect("encodes");
66            let back: MemoryRecord = decode_named(&bytes).expect("decodes");
67            assert_eq!(back, record);
68        }
69    }
70}