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(
23        &self,
24        session_id: &str,
25        json_line: &str,
26    ) -> io::Result<()> {
27        let _ = (session_id, json_line);
28        Ok(())
29    }
30}
31
32#[async_trait::async_trait]
33impl<T: RuntimeSessionPersistence + ?Sized> RuntimeSessionPersistence for Arc<T> {
34    async fn save_runtime_session(&self, session: &mut Session) -> io::Result<()> {
35        (**self).save_runtime_session(session).await
36    }
37
38    async fn append_token_usage_record(
39        &self,
40        session_id: &str,
41        json_line: &str,
42    ) -> io::Result<()> {
43        (**self)
44            .append_token_usage_record(session_id, json_line)
45            .await
46    }
47}