From d819fa314cb71359fd7a907606aba68088191811 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 27 Nov 2026 19:13:02 +0100 Subject: [PATCH] Persona trace: the turn record is the stored row; TurnTrace and TraceItem v1 are deleted FileTrace keeps each record as a CultCache entry keyed -t with the record Value as its payload and lists them oldest first by when. Co-Authored-By: Claude Opus 5.5 --- crates/mind-body/src/persona.rs | 62 +++++++++++++-------------- crates/mind-body/src/persona/tests.rs | 25 ++++------- 2 files changed, 38 insertions(+), 49 deletions(-) diff --git a/crates/mind-body/src/persona.rs b/crates/mind-body/src/persona.rs index 40f152e..b927825 100644 --- a/crates/mind-body/src/persona.rs +++ b/crates/mind-body/src/persona.rs @@ -8,7 +8,7 @@ //! //! What the module holds for that turn is what a chat line is made of: her `Config`, who a sender is to her //! (`sender_role`), whom a message addresses (`addressees`, `addressing_of`), the framed `message` line, the tail of -//! her rooms (`render_tail`) and the turn trace's store (`FileTrace`). Items no start calls yet carry an +//! her rooms (`render_tail`) and the turn records' store (`FileTrace`). Items no start calls yet carry an //! `allow(dead_code)` that the first caller removes. use std::collections::BTreeMap; @@ -20,6 +20,7 @@ use chrono::{DateTime, Utc}; use cultcache_rs::{CultCache, DatabaseEntry, SingleFileMessagePackBackingStore}; use eureka_pipeline::{MemoryOrigin, Slug}; use serde::{Deserialize, Serialize}; +use serde_json::Value; use crate::address::{Addressee, addresses, is_for, localpart}; use crate::chat_client::{Basis, ChatDelivery, TailLine}; @@ -127,36 +128,21 @@ impl Config { } } -/// Where a turn's record goes. +/// Where a turn's record goes: the record itself, the shape `projection::outcome::outcome_of` reads, keyed `-t`. +/// Her last outcome, her recent selections, her exchange records and her last wake are read back from it +/// (`projection::turn::history`); nothing else holds them. pub trait Trace { - fn record(&self, trace: &TurnTrace) -> Result<()>; + fn record(&self, key: &str, record: &Value) -> Result<()>; + /// Every record, oldest first. + fn records(&self) -> Result>; } -/// One item of a trace with the provenance it came in under: `operator`, -/// `member`, `persona` for what she was shown; `her own` for what she produced. -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct TraceItem { - pub provenance: String, - pub room: String, - pub text: String, -} - -/// The record of one turn: what went in, what came out, how it ended. -#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] -#[cultcache(type = "eureka.persona.turn_trace.v1", schema = "PersonaTurnTrace")] -pub struct TurnTrace { +/// One stored record: the turn's record, whole. +#[derive(Clone, Debug, PartialEq, DatabaseEntry)] +#[cultcache(type = "eureka.persona.turn_record.v1", schema = "PersonaTurnRecord")] +pub struct TurnRecord { #[cultcache(key = 0)] - pub run: String, - #[cultcache(key = 1)] - pub turn: u32, - #[cultcache(key = 2)] - pub at: DateTime, - #[cultcache(key = 3)] - pub inputs: Vec, - #[cultcache(key = 4)] - pub outputs: Vec, - #[cultcache(key = 5)] - pub outcome: String, + pub value: Value, } /// The trace in a CultCache single-file store under the unit's state directory. @@ -168,17 +154,27 @@ impl FileTrace { pub fn new(path: impl Into) -> Self { Self { path: path.into() } } + + fn open(&self) -> Result { + let mut cache = CultCache::new(); + cache.register_entry_type::()?; + cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&self.path))?; + cache.pull_all_backing_stores()?; + Ok(cache) + } } impl Trace for FileTrace { - fn record(&self, trace: &TurnTrace) -> Result<()> { - let mut cache = CultCache::new(); - cache.register_entry_type::()?; - cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&self.path))?; - cache.pull_all_backing_stores()?; - cache.put(format!("{}-{}", trace.run, trace.turn), trace)?; + fn record(&self, key: &str, record: &Value) -> Result<()> { + self.open()?.put(key.to_owned(), &TurnRecord { value: record.clone() })?; Ok(()) } + + fn records(&self) -> Result> { + let mut rows: Vec<(String, String, Value)> = self.open()?.get_all_with_keys::()?.into_iter().map(|(key, row)| (row.value["at"].as_str().unwrap_or_default().to_owned(), key, row.value)).collect(); + rows.sort_by(|a, b| (&a.0, &a.1).cmp(&(&b.0, &b.1))); + Ok(rows.into_iter().map(|(_, _, value)| value).collect()) + } } /// What a start did. Every outcome is an answer. diff --git a/crates/mind-body/src/persona/tests.rs b/crates/mind-body/src/persona/tests.rs index 56795c3..3b5a986 100644 --- a/crates/mind-body/src/persona/tests.rs +++ b/crates/mind-body/src/persona/tests.rs @@ -171,23 +171,16 @@ fn her_own_row_is_in_the_addressing_exactly_once_whatever_the_table_holds() { } #[test] -fn the_trace_store_keeps_each_turn_under_its_run_and_turn() { +fn the_trace_store_keeps_each_record_under_its_key_and_lists_them_oldest_first() { let dir = TempDir::new().unwrap(); - let path = dir.path().join("persona-trace.cc"); - let trace = TurnTrace { - run: "mind-1".into(), - turn: 2, - at: at(0), - inputs: vec![TraceItem { provenance: "member".into(), room: "swarm".into(), text: "hi".into() }], - outputs: vec![], - outcome: "answered".into(), - }; - FileTrace::new(&path).record(&trace).unwrap(); - let mut cache = CultCache::new(); - cache.register_entry_type::().unwrap(); - cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path)).unwrap(); - cache.pull_all_backing_stores().unwrap(); - assert_eq!(cache.get::("mind-1-2").unwrap(), Some(trace)); + let trace = FileTrace::new(dir.path().join("persona-trace.cc")); + let record = |turn: u32, minute: i64| serde_json::json!({ "at": at(minute).to_rfc3339(), "posted": [], "turn": turn }); + trace.record("mind-1-t10", &record(10, 3)).unwrap(); + trace.record("mind-1-t2", &record(2, 1)).unwrap(); + trace.record("mind-1-t3", &record(3, 2)).unwrap(); + let turns: Vec = trace.records().unwrap().iter().map(|record| record["turn"].as_u64().unwrap()).collect(); + assert_eq!(turns, [2, 3, 10], "oldest first by when, not by key"); + assert_eq!(trace.records().unwrap()[0], record(2, 1), "the record is stored whole"); } #[test]