lunaris-core 0.8.0

Core types, traits, and bi-temporal primitives for the Lunaris agent memory engine
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
//! Six bi-temporal primitives — verbatim per blueprint §3.3.
//!
//! Every primitive carries a `BiTemporal { valid, sys }` stamp from a shared `HlcClock`.
//! Every primitive is `Send + Sync + 'static`, `Debug`, `Clone`, `PartialEq`, and serde-roundtrippable.
//!
//! RFC 0001 (v0.2): every primitive now carries `pub scope: Scope` as a first-class
//! partition key for multi-agent / multi-tenant isolation. Constructors take `scope`
//! as the first argument. Existing call sites use `Scope::dev()` during the Wave 0
//! migration; Wave 1 replaces those with real per-agent scopes.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use ulid::Ulid;

use crate::bitemporal::BiTemporal;
use crate::hlc::{Hlc, HlcClock};
use crate::scope::Scope;

// ---------------- Episode ----------------

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Episode {
    pub id: Ulid,
    /// RFC 0001 — partition key for multi-agent / multi-tenant isolation.
    pub scope: Scope,
    pub source: String,
    pub content: String,
    pub t_ref: Option<DateTime<Utc>>,
    pub bt: BiTemporal,
    #[serde(default)]
    pub metadata: serde_json::Map<String, serde_json::Value>,
}

impl Episode {
    /// Ground the **valid** axis on `t_ref`, leaving the **system** axis alone.
    ///
    /// `t_ref` is the caller's declared real-world date for this content —
    /// a chat session's date, a commit's author date, a document's dateline.
    /// [`Episode::new`] cannot know it, so it stamps `BiTemporal::now`, which
    /// puts BOTH axes on the ingest instant. Until this runs, the store is
    /// mono-temporal with a spare field: `Filter::ValidTimeRange` answers
    /// "what did we WRITE in this window" instead of "what was TRUE in this
    /// window", and a corpus of last year's events matches nothing dated last
    /// year (F21).
    ///
    /// Idempotent, and a no-op without a `t_ref` — an undated episode has
    /// nothing better to say than "now", and saying "now" is correct rather
    /// than merely a fallback.
    ///
    /// The system axis is deliberately untouched. It records when Lunaris
    /// learned the thing, and no caller-supplied value may move it: an
    /// `as_of` system query that could be talked into claiming we knew
    /// something before we recorded it is not an audit trail.
    pub fn ground_valid_axis(&mut self) {
        if let Some(t) = self.t_ref {
            self.bt.valid.0 = Hlc::from_utc(t);
        }
    }

    /// Construct a new [`Episode`].
    ///
    /// `scope` is the partition key (RFC 0001). Use [`Scope::dev()`] at Wave 0
    /// call sites where the real scope has not yet been threaded through.
    pub fn new(
        scope: Scope,
        source: impl Into<String>,
        content: impl Into<String>,
        clock: &HlcClock,
    ) -> Self {
        Self {
            id: Ulid::new(),
            scope,
            source: source.into(),
            content: content.into(),
            t_ref: None,
            bt: BiTemporal::now(clock),
            metadata: serde_json::Map::new(),
        }
    }
}

// ---------------- Chunk ----------------

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Chunk {
    pub id: Ulid,
    /// RFC 0001 — partition key, inherited from the parent [`Episode`].
    pub scope: Scope,
    pub episode_id: Ulid,
    pub text: String,
    pub tokens: u32,
    pub offset: u32,
    #[serde(default)]
    pub heading_path: Vec<String>,
    #[serde(default)]
    pub overlap_tail: String,
    /// The 768-d embedding vector.
    ///
    /// ## W3 embedding double-store fix (moon-v051-perf-exploit)
    ///
    /// `skip_serializing` is deliberate: the KV `KvPut` JSON payload used to
    /// carry this as a raw JSON float array — ~80% of the document's bytes
    /// and a straight duplicate of the binary vector Moon's FT index
    /// already stores. Nothing
    /// on the read path (`lunaris-retrieve::hydrate`, the `tree.rs` RAPTOR
    /// descent, the `detail.rs` inspector route) reads `.embedding` back off
    /// a KV-deserialized primitive — verified via `find_referencing_symbols`
    /// before this cut.
    ///
    /// `#[serde(default)]` keeps deserialization tolerant of BOTH shapes:
    /// legacy payloads written before this fix (field present) still
    /// populate `Some(..)`; payloads written after it (field absent)
    /// deserialize to `None`. This is a one-way, additive-compatible
    /// serialize-side change — never a breaking wire format change.
    #[serde(default, skip_serializing)]
    pub embedding: Option<Vec<f32>>,
    /// Optional link to the nearest parent `TocNode` in the document tree.
    ///
    /// `None` in Phase 27 (field + migration + serde-compat delivered here;
    /// full parent wiring — setting a non-None value — lands in Phase 29).
    /// Pre-existing rows serialised without this field deserialise to `None`
    /// via `#[serde(default)]` (STRUCT-03 serde back-compat contract).
    ///
    /// Travels in the existing JSONB KvPut payload — no DDL, no index change.
    /// (Through 0.6.x the Postgres backend carried it as
    /// `chunks.parent_id BYTEA NULL`; that backend was removed in 0.7.0.)
    #[serde(default)]
    pub parent_id: Option<Ulid>,
    pub bt: BiTemporal,
}

impl Chunk {
    /// Construct a new [`Chunk`].
    ///
    /// `scope` must match the parent [`Episode::scope`] (RFC 0001 §3.2).
    pub fn new(
        scope: Scope,
        episode_id: Ulid,
        text: impl Into<String>,
        tokens: u32,
        offset: u32,
        heading_path: Vec<String>,
        clock: &HlcClock,
    ) -> Self {
        Self {
            id: Ulid::new(),
            scope,
            episode_id,
            text: text.into(),
            tokens,
            offset,
            heading_path,
            overlap_tail: String::new(),
            embedding: None,
            parent_id: None,
            bt: BiTemporal::now(clock),
        }
    }
}

// ---------------- Entity ----------------

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Entity {
    pub id: Ulid,
    /// RFC 0001 — partition key for multi-agent / multi-tenant isolation.
    pub scope: Scope,
    pub name: String,
    #[serde(default)]
    pub aliases: Vec<String>,
    pub entity_type: String,
    /// See the W3 skip_serializing rationale on [`Chunk::embedding`].
    #[serde(default, skip_serializing)]
    pub embedding: Option<Vec<f32>>,
    pub bt: BiTemporal,
    pub confidence: f32,
}

impl Entity {
    /// Construct a new [`Entity`].
    ///
    /// `scope` is the partition key (RFC 0001). `src` and `dst` of any
    /// [`Relation`] referencing this entity MUST share the same scope —
    /// cross-scope relations are disallowed by construction in v0.2.
    pub fn new(
        scope: Scope,
        name: impl Into<String>,
        entity_type: impl Into<String>,
        confidence: f32,
        clock: &HlcClock,
    ) -> Self {
        Self {
            id: Ulid::new(),
            scope,
            name: name.into(),
            aliases: Vec::new(),
            entity_type: entity_type.into(),
            embedding: None,
            bt: BiTemporal::now(clock),
            confidence,
        }
    }
}

// ---------------- Relation ----------------

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Relation {
    pub id: Ulid,
    /// RFC 0001 — partition key. `src` and `dst` MUST resolve within this scope.
    pub scope: Scope,
    pub src: Ulid,
    pub dst: Ulid,
    pub rel_type: String,
    pub bt: BiTemporal,
    pub confidence: f32,
    #[serde(default)]
    pub provenance: Vec<Ulid>,
}

impl Relation {
    /// Construct a new [`Relation`].
    ///
    /// `src` and `dst` MUST resolve within `scope` — cross-scope graph
    /// references are disallowed by construction in v0.2 (RFC 0001 §2.3).
    pub fn new(
        scope: Scope,
        src: Ulid,
        dst: Ulid,
        rel_type: impl Into<String>,
        confidence: f32,
        clock: &HlcClock,
    ) -> Self {
        Self {
            id: Ulid::new(),
            scope,
            src,
            dst,
            rel_type: rel_type.into(),
            bt: BiTemporal::now(clock),
            confidence,
            provenance: Vec::new(),
        }
    }
}

// ---------------- Fact ----------------

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Fact {
    pub id: Ulid,
    /// RFC 0001 — partition key for multi-agent / multi-tenant isolation.
    pub scope: Scope,
    pub subject: Ulid,
    pub predicate: String,
    pub object: Ulid,
    pub fact_text: String,
    /// See the W3 skip_serializing rationale on [`Chunk::embedding`].
    #[serde(default, skip_serializing)]
    pub embedding: Option<Vec<f32>>,
    pub bt: BiTemporal,
    pub confidence: f32,
    #[serde(default)]
    pub provenance: Vec<Ulid>,
    pub activation: f32,
}

impl Fact {
    /// Construct a new [`Fact`].
    ///
    /// `scope` is the partition key (RFC 0001). Corresponds to "Claim" in the
    /// RFC §3.2 primitive list (the codebase uses `Fact` as the canonical name).
    pub fn new(
        scope: Scope,
        subject: Ulid,
        predicate: impl Into<String>,
        object: Ulid,
        fact_text: impl Into<String>,
        confidence: f32,
        clock: &HlcClock,
    ) -> Self {
        Self {
            id: Ulid::new(),
            scope,
            subject,
            predicate: predicate.into(),
            object,
            fact_text: fact_text.into(),
            embedding: None,
            bt: BiTemporal::now(clock),
            confidence,
            provenance: Vec::new(),
            activation: 0.0,
        }
    }
}

// ---------------- Community ----------------

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Community {
    pub id: Ulid,
    /// RFC 0001 — partition key. Corresponds to "Source" in the RFC §3.2
    /// primitive list (the codebase uses `Community` as the canonical name).
    pub scope: Scope,
    pub level: u8,
    pub parent: Option<Ulid>,
    #[serde(default)]
    pub members: Vec<Ulid>,
    pub summary: String,
    /// See the W3 skip_serializing rationale on [`Chunk::embedding`]. Populated
    /// in-memory at ingest (Phase-30 B1) and written to the `communities`
    /// vector index; never round-trips through the KV JSON blob.
    #[serde(default, skip_serializing)]
    pub summary_embedding: Option<Vec<f32>>,
    pub bt: BiTemporal,
}

impl Community {
    /// Construct a new [`Community`].
    ///
    /// `scope` is the partition key (RFC 0001). All member entity IDs in
    /// `members` MUST resolve within this scope.
    pub fn new(scope: Scope, level: u8, summary: impl Into<String>, clock: &HlcClock) -> Self {
        Self {
            id: Ulid::new(),
            scope,
            level,
            parent: None,
            members: Vec::new(),
            summary: summary.into(),
            summary_embedding: None,
            bt: BiTemporal::now(clock),
        }
    }
}

// ---------------------------------------------------------------------------
// STRUCT-03 tests — Chunk.parent_id serde back-compat + construction
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hlc::HlcClock;
    use crate::scope::Scope;

    fn dev_scope() -> Scope {
        Scope::dev()
    }

    fn test_clock() -> std::sync::Arc<HlcClock> {
        HlcClock::new(0)
    }

    #[test]
    fn chunk_deserializes_without_parent_id() {
        // Prove serde back-compat: a JSON row serialized without "parent_id"
        // (pre-Phase 27 row) must deserialize successfully with parent_id=None.
        // Strategy: serialize a real Chunk, remove "parent_id" from the JSON
        // object, then deserialize — format-agnostic, no hardcoded BiTemporal layout.
        let clock = test_clock();
        let chunk = Chunk::new(dev_scope(), ulid::Ulid::new(), "hello world", 2, 0, vec![], &clock);
        let mut map: serde_json::Map<String, serde_json::Value> =
            serde_json::from_str(&serde_json::to_string(&chunk).unwrap()).unwrap();
        // Simulate a pre-Phase-27 row by dropping the parent_id field entirely.
        map.remove("parent_id");
        let stripped = serde_json::to_string(&map).unwrap();
        let back: Chunk = serde_json::from_str(&stripped)
            .expect("back-compat: must deserialize without parent_id");
        assert!(chunk.parent_id.is_none(), "parent_id must be None for pre-existing rows");
        assert_eq!(back.text, chunk.text);
    }

    #[test]
    fn chunk_deserializes_with_parent_id() {
        // Prove that a row WITH parent_id set deserializes correctly.
        let clock = test_clock();
        let mut chunk =
            Chunk::new(dev_scope(), ulid::Ulid::new(), "hello world", 2, 0, vec![], &clock);
        let parent = ulid::Ulid::new();
        chunk.parent_id = Some(parent);
        let json = serde_json::to_string(&chunk).unwrap();
        let back: Chunk = serde_json::from_str(&json).expect("must deserialize with parent_id");
        assert_eq!(back.parent_id, Some(parent), "parent_id must be Some when present in JSON");
    }

    #[test]
    fn chunk_new_has_none_parent_id() {
        let clock = test_clock();
        let chunk = Chunk::new(dev_scope(), ulid::Ulid::new(), "test text", 3, 0, vec![], &clock);
        assert!(chunk.parent_id.is_none(), "Chunk::new must produce parent_id = None");
    }

    #[test]
    fn chunk_with_parent_id_roundtrips_serde() {
        let clock = test_clock();
        let mut chunk = Chunk::new(
            dev_scope(),
            ulid::Ulid::new(),
            "test text",
            3,
            0,
            vec!["Section 1".to_string()],
            &clock,
        );
        let parent = ulid::Ulid::new();
        chunk.parent_id = Some(parent);

        let json = serde_json::to_string(&chunk).expect("serialize");
        let back: Chunk = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back.parent_id, Some(parent));
        assert_eq!(back.text, chunk.text);
    }
}