1use std::collections::{BTreeMap, HashMap, HashSet};
6use std::io;
7use std::ops::ControlFlow;
8
9use rig_core::conversation::{ChangeRecord, Conversation, Lineage};
10use serde_json::Value;
11
12use crate::blobs::{BLOBS, load_images_with, messages_mut, named_blobs, put_blob, read_blob};
13use crate::format::{Body, Header, LogLines, Meta, SESSION, parse, stamp_of};
14use crate::store::now;
15use crate::writer::Book;
16use crate::{FORMAT, LogRewrite, Repair, SessionError, Stamp, Store, check_log_name};
17
18#[derive(Clone, Debug, Default, PartialEq)]
20pub struct Session {
21 pub agents: Vec<SavedAgent>,
23 pub saved: BTreeMap<String, Value>,
25 pub repairs: Vec<Repair>,
27 pub(crate) ends: BTreeMap<String, End>,
30}
31
32pub(crate) type End = (Lineage, u64);
35
36#[derive(Clone, Debug, Default, PartialEq)]
38pub struct SavedAgent {
39 pub id: String,
41 pub parent: Option<String>,
43 pub started_ms: u64,
45 pub conversation: Conversation,
48 pub saved: BTreeMap<String, Value>,
50 pub retired: bool,
53 pub rewrites: Vec<LogRewrite>,
55}
56
57#[derive(Debug)]
59pub struct Unreadable {
60 pub log: String,
62 pub error: SessionError,
64}
65
66#[derive(Clone, Debug, PartialEq)]
68pub enum LogRecord {
69 Change(ChangeRecord),
71 Saved {
73 key: String,
75 value: Option<Value>,
77 },
78 Retired,
80}
81
82#[derive(Default)]
84pub(crate) struct Folded {
85 pub name: String,
86 pub header: Header,
87 pub started_ms: u64,
88 pub conversation: Conversation,
89 pub state: BTreeMap<String, Value>,
90 pub retired: bool,
91 pub next_seq: u64,
93 pub next_n: u64,
95 pub len: u64,
97 pub torn: Option<(u64, u64)>,
99 pub lost: Vec<io::Error>,
101}
102
103struct Read {
105 header: Header,
106 started: Stamp,
107 last: Stamp,
108}
109
110pub(crate) fn log_lines(store: &dyn Store, name: &str) -> Result<Option<LogLines>, SessionError> {
112 match store.read_log(name) {
113 Err(failure) if failure.kind() == io::ErrorKind::NotFound => Ok(None),
114 bytes => Ok(Some(LogLines::parse(bytes?))),
115 }
116}
117
118pub(crate) fn read_logs(store: &dyn Store) -> Result<Vec<(String, LogLines)>, SessionError> {
120 let mut logs = Vec::new();
121 for name in store.logs()? {
122 logs.extend(log_lines(store, &name)?.map(|lines| (name, lines)));
123 }
124 Ok(logs)
125}
126
127fn read(
129 store: &dyn Store,
130 name: &str,
131 each: impl FnMut(Stamp, Body) -> Result<ControlFlow<()>, String>,
132) -> Result<Option<Read>, SessionError> {
133 match log_lines(store, name)? {
134 Some(lines) => read_lines(name, &lines, each),
135 None => Ok(None),
136 }
137}
138
139fn read_lines(
143 name: &str,
144 lines: &LogLines,
145 mut each: impl FnMut(Stamp, Body) -> Result<ControlFlow<()>, String>,
146) -> Result<Option<Read>, SessionError> {
147 let Some((_, _, first)) = lines.iter().next() else {
148 return Ok(None);
149 };
150 let refuse = |line, why| SessionError::record(name, line, why);
151 let (started, header) = read_header(name, first)?;
152 let mut last = started;
153 for (line, _, bytes) in lines.iter().skip(1) {
154 let (stamp, body) = parse(bytes).map_err(|why| refuse(line, why))?;
155 if matches!(body, Body::Meta(Meta::Header(_))) {
156 return Err(refuse(line, "a second header".to_owned()));
157 }
158 if each(stamp, body)
159 .map_err(|why| refuse(line, why))?
160 .is_break()
161 {
162 break;
163 }
164 last = stamp;
165 }
166 Ok(Some(Read {
167 header,
168 started,
169 last,
170 }))
171}
172
173pub(crate) fn read_header(name: &str, first: &[u8]) -> Result<(Stamp, Header), SessionError> {
176 let (started, header) = match parse(first) {
177 Ok((stamp, Body::Meta(Meta::Header(header)))) => (stamp, header),
178 _ => {
179 let found = serde_json::from_slice::<Header>(first)
180 .ok()
181 .and_then(|header| header.format);
182 return Err(SessionError::Format {
183 log: name.to_owned(),
184 found,
185 });
186 }
187 };
188 if header.format.as_deref() != Some(FORMAT) {
189 let found = header.format;
190 let log = name.to_owned();
191 return Err(SessionError::Format { log, found });
192 }
193 if !header.lineage.is_log() {
194 let why = "a header without the log's lineage".to_owned();
195 return Err(SessionError::record(name, 1, why));
196 }
197 Ok((started, header))
198}
199
200#[derive(Clone, Copy, Default)]
202pub(crate) struct Fold {
203 pub until: Option<u64>,
205 pub images: bool,
207}
208
209impl Fold {
210 pub const WHOLE: Self = Self {
212 until: None,
213 images: true,
214 };
215}
216
217pub(crate) fn fold(
221 store: &dyn Store,
222 name: &str,
223 how: Fold,
224) -> Result<Option<Folded>, SessionError> {
225 match log_lines(store, name)? {
226 Some(lines) => fold_lines(name, &lines, how, &|blob| read_blob(store, blob)),
227 None => Ok(None),
228 }
229}
230
231pub(crate) fn fold_lines(
234 name: &str,
235 lines: &LogLines,
236 how: Fold,
237 blob: &dyn Fn(&str) -> io::Result<Vec<u8>>,
238) -> Result<Option<Folded>, SessionError> {
239 fold_lines_with(name, lines, (how, blob), &mut |_, _| {})
240}
241
242fn fold_lines_with(
245 name: &str,
246 lines: &LogLines,
247 (how, blob): (Fold, &dyn Fn(&str) -> io::Result<Vec<u8>>),
248 each: &mut dyn FnMut(&Conversation, &ChangeRecord),
249) -> Result<Option<Folded>, SessionError> {
250 let mut conversation = Conversation::new();
251 let mut state = BTreeMap::new();
252 let mut retired = false;
253 let mut lost = Vec::new();
254 let read = read_lines(name, lines, |stamp, body| {
255 if how.until.is_some_and(|until| stamp.n >= until) {
256 return Ok(ControlFlow::Break(()));
257 }
258 match body {
259 Body::Meta(meta) => note_meta(meta, &mut state, &mut retired),
260 Body::Change(mut recorded) => {
261 if how.images {
262 let _ = messages_mut(&mut recorded.change, &mut |message| {
263 if let Err(failure) = load_images_with(message, blob) {
264 lost.push(failure);
265 }
266 Ok(())
267 });
268 }
269 each(&conversation, &recorded);
270 conversation
271 .apply(&recorded)
272 .map_err(|error| error.to_string())?;
273 }
274 }
275 Ok(ControlFlow::Continue(()))
276 })?;
277 let Some(read) = read else {
278 return Ok(None);
279 };
280 let changes = conversation.revision();
281 conversation.continue_log(read.header.lineage, changes);
282 let tail = lines.tail().len() as u64;
283 Ok(Some(Folded {
284 name: name.to_owned(),
285 started_ms: read.started.t,
286 header: read.header,
287 conversation,
288 state,
289 retired,
290 next_seq: read.last.seq.saturating_add(1),
291 next_n: read.last.n.saturating_add(1),
292 len: lines.len(),
293 torn: (tail > 0).then(|| (lines.len(), tail)),
294 lost,
295 }))
296}
297
298pub(crate) fn note_meta(meta: Meta, state: &mut BTreeMap<String, Value>, retired: &mut bool) {
301 match meta {
302 Meta::State { key, value } => {
303 match value.filter(|value| !value.is_null()) {
304 Some(value) => state.insert(key, value),
305 None => state.remove(&key),
306 };
307 }
308 Meta::Retired => *retired = true,
309 Meta::Header(_) => {}
310 }
311}
312
313fn tally(name: &str, lines: &LogLines) -> Result<Option<Folded>, SessionError> {
319 let Some((_, _, first)) = lines.iter().next() else {
320 return Ok(None);
321 };
322 let (started, header) = read_header(name, first)?;
323 let (mut last, mut changes) = (started, 0);
324 let (mut state, mut retired) = (BTreeMap::new(), false);
325 for (line, _, bytes) in lines.iter().skip(1) {
326 let refuse = |why| SessionError::record(name, line, why);
327 let (stamp, change) = stamp_of(bytes).map_err(refuse)?;
328 if !change && let (_, Body::Meta(meta)) = parse(bytes).map_err(refuse)? {
329 if matches!(meta, Meta::Header(_)) {
330 return Err(refuse("a second header".to_owned()));
331 }
332 note_meta(meta, &mut state, &mut retired);
333 }
334 last = stamp;
335 changes += u64::from(change);
336 }
337 let mut conversation = Conversation::new();
338 conversation.continue_log(header.lineage, changes);
339 let tail = lines.tail().len() as u64;
340 Ok(Some(Folded {
341 name: name.to_owned(),
342 started_ms: started.t,
343 header,
344 conversation,
345 state,
346 retired,
347 next_seq: last.seq.saturating_add(1),
348 next_n: last.n.saturating_add(1),
349 len: lines.len(),
350 torn: (tail > 0).then(|| (lines.len(), tail)),
351 ..Folded::default()
352 }))
353}
354
355pub(crate) fn fold_all(
358 store: &dyn Store,
359 logs: &[(String, LogLines)],
360) -> Result<Vec<Folded>, SessionError> {
361 let mut folded = Vec::new();
362 for (name, lines) in logs {
363 folded.extend(fold_lines(name, lines, Fold::WHOLE, &|blob| {
364 read_blob(store, blob)
365 })?);
366 }
367 Ok(folded)
368}
369
370pub(crate) fn repairs(store: &dyn Store, folded: &[Folded]) -> Result<Vec<Repair>, SessionError> {
373 let torn = torn_tails(store, folded)?.into_iter();
374 let mut repairs: Vec<Repair> = torn
375 .map(|(log, kept, cut)| Repair::TornTail {
376 log,
377 kept,
378 cut,
379 set_aside: None,
380 })
381 .collect();
382 for log in folded {
383 repairs.extend(log.lost.iter().map(|why| Repair::LostImage {
384 log: log.name.clone(),
385 why: why.to_string(),
386 }));
387 }
388 Ok(repairs)
389}
390
391pub(crate) fn torn_tails(
396 store: &dyn Store,
397 folded: &[Folded],
398) -> Result<Vec<(String, u64, u64)>, SessionError> {
399 let mut torn = Vec::new();
400 for log in store.logs()? {
401 match folded.iter().find(|folded| folded.name == log) {
402 Some(folded) => torn.extend(folded.torn.map(|(kept, cut)| (log, kept, cut))),
403 None => torn.extend(torn_whole(store, &log).map(|cut| (log, 0, cut))),
404 }
405 }
406 Ok(torn)
407}
408
409fn torn_whole(store: &dyn Store, log: &str) -> Option<u64> {
412 let lines = log_lines(store, log).ok().flatten();
414 let lines = lines.filter(|lines| lines.len() == 0)?;
415 let cut = lines.tail().len() as u64;
416 (cut > 0).then_some(cut)
417}
418
419pub(crate) fn retired(logs: &[Folded]) -> HashSet<String> {
422 let logs = logs.iter().map(|log| {
423 let parent = log.header.parent.as_deref();
424 (log.name.as_str(), parent, log.retired)
425 });
426 skipped(logs).into_iter().map(str::to_owned).collect()
427}
428
429pub(crate) fn skipped<'a>(
432 logs: impl Iterator<Item = (&'a str, Option<&'a str>, bool)>,
433) -> HashSet<&'a str> {
434 let logs: Vec<_> = logs.collect();
435 let parents: HashMap<&str, Option<&str>> = logs
436 .iter()
437 .map(|&(name, parent, _)| (name, parent))
438 .collect();
439 let gone: HashSet<&str> = logs
440 .iter()
441 .filter(|&&(_, _, retired)| retired)
442 .map(|&(name, ..)| name)
443 .collect();
444 let mut skipped = HashSet::new();
445 for &(name, ..) in &logs {
446 let up = std::iter::successors(Some(name), |id| parents.get(id).copied().flatten());
448 if up.take(logs.len() + 1).any(|id| gone.contains(id)) {
449 skipped.insert(name);
450 }
451 }
452 skipped
453}
454
455pub(crate) fn ends(folded: &[Folded]) -> BTreeMap<String, End> {
457 let end = |log: &Folded| (log.name.clone(), (log.header.lineage, log.next_seq));
458 folded.iter().map(end).collect()
459}
460
461pub(crate) fn session_of(folded: Vec<Folded>, repairs: Vec<Repair>) -> Session {
463 let mut session = Session {
464 repairs,
465 ends: ends(&folded),
466 ..Session::default()
467 };
468 for log in folded {
469 if log.name == SESSION {
470 session.saved = log.state;
471 continue;
472 }
473 session.agents.push(SavedAgent {
474 id: log.name,
475 parent: log.header.parent,
476 started_ms: log.started_ms,
477 conversation: log.conversation,
478 saved: log.state,
479 retired: log.retired,
480 rewrites: log.header.rewrites,
481 });
482 }
483 session
484 .agents
485 .sort_by(|a, b| (a.started_ms, &a.id).cmp(&(b.started_ms, &b.id)));
486 session
487}
488
489impl Session {
490 pub fn read(store: &dyn Store) -> Result<Self, SessionError> {
499 let folded = fold_all(store, &read_logs(store)?)?;
500 let repairs = repairs(store, &folded)?;
501 Ok(session_of(folded, repairs))
502 }
503
504 pub fn read_lenient(store: &dyn Store) -> Result<(Self, Vec<Unreadable>), SessionError> {
514 let mut folded = Vec::new();
515 let mut unreadable = Vec::new();
516 for log in store.logs()? {
517 match fold(store, &log, Fold::WHOLE) {
518 Ok(read) => folded.extend(read),
519 Err(error) => unreadable.push(Unreadable { log, error }),
520 }
521 }
522 let repairs = repairs(store, &folded)?;
523 Ok((session_of(folded, repairs), unreadable))
524 }
525
526 pub fn agent(&self, id: &str) -> Option<&SavedAgent> {
528 self.agents.iter().find(|agent| agent.id == id)
529 }
530
531 pub fn agent_mut(&mut self, id: &str) -> Option<&mut SavedAgent> {
533 self.agents.iter_mut().find(|agent| agent.id == id)
534 }
535
536 pub fn save(&mut self, store: &dyn Store) -> Result<(), SessionError> {
558 let mut folded = Vec::new();
559 for (name, lines) in &read_logs(store)? {
560 folded.extend(tally(name, lines)?);
561 }
562 let now = ends(&folded);
563 if !now.is_empty() && now != self.ends {
564 let mut logs = now.keys().chain(self.ends.keys());
565 let log = logs.find(|log| now.get(*log) != self.ends.get(*log));
566 let log = log.cloned().unwrap_or_default();
567 return Err(SessionError::Changed { log });
568 }
569 let had = |id: &str| folded.iter().find(|log| log.name == id);
570 for agent in &self.agents {
571 if agent.id == SESSION {
572 let why = format!("{SESSION:?} is the session's own log, not an agent's");
573 return Err(io::Error::new(io::ErrorKind::InvalidInput, why).into());
574 }
575 check_continues(had(&agent.id), &agent.id, &agent.conversation)?;
576 }
577 let mut book = Book::after(&folded);
578 let torn = torn_tails(store, &folded)?.into_iter();
579 book.set_aside(torn.map(|(log, _, cut)| (log, cut)));
580 book.start();
581 let mut staged: Vec<_> = self.agents.iter().map(|a| a.conversation.clone()).collect();
582 for (agent, conversation) in self.agents.iter().zip(&mut staged) {
583 let _ = book.opened(&agent.id, agent.parent.as_deref());
584 let put = &mut |name: String, bytes: Vec<u8>| put_blob(store, &name, &bytes);
585 book.save(put, &agent.id, conversation);
586 let known = had(&agent.id).map(|log| &log.state);
587 book.states(&agent.id, known, &agent.saved);
588 if agent.retired && !had(&agent.id).is_some_and(|log| log.retired) {
589 book.retire(&agent.id);
590 }
591 }
592 let known = had(SESSION).map(|log| &log.state);
593 book.states(SESSION, known, &self.saved);
594 let flushed = book.flush(store);
595 self.ends = now.clone();
596 self.ends.extend(book.written());
597 for (agent, conversation) in self.agents.iter_mut().zip(staged) {
598 if flushed.is_ok() || self.ends.get(&agent.id) != now.get(&agent.id) {
600 agent.conversation = conversation;
601 }
602 }
603 flushed
604 }
605}
606
607fn check_continues(
611 log: Option<&Folded>,
612 name: &str,
613 conversation: &Conversation,
614) -> Result<(), SessionError> {
615 let Some(log) = log else {
616 return Ok(());
617 };
618 let changes = log.conversation.revision();
619 if changes > 0 && !conversation.continues(log.header.lineage, changes) {
620 return Err(SessionError::Changed {
621 log: name.to_owned(),
622 });
623 }
624 Ok(())
625}
626
627pub fn load(store: &dyn Store, log: &str) -> Result<Conversation, SessionError> {
641 check_log_name(log)?;
642 conversation_of(fold(store, log, Fold::WHOLE)?)
643}
644
645pub(crate) fn conversation_of(folded: Option<Folded>) -> Result<Conversation, SessionError> {
649 let Some(folded) = folded else {
650 return Ok(Conversation::new());
651 };
652 let mut lost = folded.lost.into_iter();
653 match lost.find(|failure| failure.kind() != io::ErrorKind::NotFound) {
654 Some(failure) => Err(failure.into()),
655 None => Ok(folded.conversation),
656 }
657}
658
659pub fn save(
689 store: &dyn Store,
690 log: &str,
691 conversation: &mut Conversation,
692) -> Result<(), SessionError> {
693 now(save_async(store, log, conversation))
694}
695
696pub async fn load_async<S: crate::AsyncStore + ?Sized>(
703 store: &S,
704 log: &str,
705) -> Result<Conversation, SessionError> {
706 check_log_name(log)?;
707 let bytes = match store.read_log_async(log).await {
708 Err(failure) if failure.kind() == io::ErrorKind::NotFound => {
709 return Ok(Conversation::new());
710 }
711 bytes => bytes?,
712 };
713 let mut named = std::collections::BTreeSet::new();
714 named_blobs(&bytes, &mut named);
715 let mut blobs = BTreeMap::new();
716 for name in named {
717 let read = store.read_file_async(&format!("{BLOBS}{name}")).await;
718 blobs.insert(name, read);
719 }
720 let blob = |name: &str| match blobs.get(name) {
721 Some(Ok(bytes)) => Ok(bytes.clone()),
722 Some(Err(failure)) => Err(io::Error::new(failure.kind(), failure.to_string())),
723 None => Err(io::ErrorKind::NotFound.into()),
724 };
725 conversation_of(fold_lines(
726 log,
727 &LogLines::parse(bytes),
728 Fold::WHOLE,
729 &blob,
730 )?)
731}
732
733pub async fn save_async<S: crate::AsyncStore + ?Sized>(
761 store: &S,
762 log: &str,
763 conversation: &mut Conversation,
764) -> Result<(), SessionError> {
765 let lines = match store.read_log_async(log).await {
766 Err(failure) if failure.kind() == io::ErrorKind::NotFound => LogLines::parse(Vec::new()),
767 bytes => LogLines::parse(bytes?),
768 };
769 let folded = tally(log, &lines)?;
770 check_continues(folded.as_ref(), log, conversation)?;
771 let mut book = Book::after(folded.as_slice());
772 let cut = lines.tail().len() as u64;
775 book.set_aside((cut > 0).then(|| (log.to_owned(), cut)));
776 let mut staged = conversation.clone();
777 book.start();
778 let _ = book.opened(log, None);
779 let mut blobs = Vec::new();
780 let put = &mut |name, bytes| {
781 blobs.push((name, bytes));
782 Ok(())
783 };
784 book.save(put, log, &mut staged);
785 for (name, bytes) in blobs {
786 store
787 .write_file_async(&format!("{BLOBS}{name}"), &bytes)
788 .await?;
789 }
790 book.flush_async(store).await?;
791 *conversation = staged;
792 Ok(())
793}
794
795pub fn history(store: &dyn Store, log: &str) -> Result<Vec<(Stamp, LogRecord)>, SessionError> {
803 let mut records = Vec::new();
804 read(store, log, |stamp, body| {
805 let record = match body {
806 Body::Change(mut recorded) => {
807 let _ = messages_mut(&mut recorded.change, &mut |message| {
809 let _ = load_images_with(message, &|name| read_blob(store, name));
810 Ok(())
811 });
812 LogRecord::Change(recorded)
813 }
814 Body::Meta(Meta::State { key, value }) => LogRecord::Saved {
815 key,
816 value: value.filter(|value| !value.is_null()),
817 },
818 Body::Meta(Meta::Retired) => LogRecord::Retired,
819 Body::Meta(Meta::Header(_)) => return Ok(ControlFlow::Continue(())),
820 };
821 records.push((stamp, record));
822 Ok(ControlFlow::Continue(()))
823 })?;
824 Ok(records)
825}
826
827pub(crate) fn replay(
835 store: &dyn Store,
836 log: &str,
837 mut each: impl FnMut(&Conversation, &ChangeRecord),
838) -> Result<(), SessionError> {
839 if let Some(lines) = log_lines(store, log)? {
840 let blob = |blob: &str| read_blob(store, blob);
841 fold_lines_with(log, &lines, (Fold::WHOLE, &blob), &mut each)?;
842 }
843 Ok(())
844}
845
846pub fn as_of(store: &dyn Store, log: &str, n: u64) -> Result<Conversation, SessionError> {
854 let how = Fold {
855 until: Some(n),
856 ..Fold::WHOLE
857 };
858 conversation_of(fold(store, log, how)?)
859}
860#[cfg(test)]
861mod tests;