Skip to main content

bamboo_domain/session/
persistence.rs

1use std::io;
2use std::sync::Arc;
3
4use crate::session::types::Session;
5
6/// Port for runtime (non-authoritative) session persistence.
7///
8/// Implementors must:
9/// - Serialize concurrent saves per session ID.
10/// - Merge on-disk authoritative metadata (`title`, `pinned`, `title_version`,
11///   `metadata_version`) before writing, so UI edits are never clobbered.
12#[async_trait::async_trait]
13pub trait RuntimeSessionPersistence: Send + Sync {
14    /// Persist the session, merging any newer authoritative metadata from disk.
15    async fn save_runtime_session(&self, session: &mut Session) -> io::Result<()>;
16
17    /// Append one JSON-line analysis record to the session's append-only
18    /// token-usage log (see [`Storage::append_token_usage_record`]). Defaults to
19    /// a no-op so non-file-backed persisters are unaffected.
20    ///
21    /// [`Storage::append_token_usage_record`]: crate::storage::Storage::append_token_usage_record
22    async fn append_token_usage_record(&self, session_id: &str, json_line: &str) -> io::Result<()> {
23        let _ = (session_id, json_line);
24        Ok(())
25    }
26}
27
28#[async_trait::async_trait]
29impl<T: RuntimeSessionPersistence + ?Sized> RuntimeSessionPersistence for Arc<T> {
30    async fn save_runtime_session(&self, session: &mut Session) -> io::Result<()> {
31        (**self).save_runtime_session(session).await
32    }
33
34    async fn append_token_usage_record(&self, session_id: &str, json_line: &str) -> io::Result<()> {
35        (**self)
36            .append_token_usage_record(session_id, json_line)
37            .await
38    }
39}