pub mod memory;
#[cfg(feature = "sqlite-store")]
pub mod sqlite;
use meerkat_core::lifecycle::{InputId, RunBoundaryReceipt, RunId};
use crate::identifiers::LogicalRuntimeId;
use crate::input_state::StoredInputState;
use crate::runtime_state::RuntimeState;
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
pub enum RuntimeStoreError {
#[error("Store write failed: {0}")]
WriteFailed(String),
#[error("Store read failed: {0}")]
ReadFailed(String),
#[error("Session store key mismatch: expected {expected}, actual {actual}")]
SessionKeyMismatch {
expected: meerkat_core::types::SessionId,
actual: meerkat_core::types::SessionId,
},
#[error("Not found: {0}")]
NotFound(String),
#[error("Unsupported store operation: {0}")]
Unsupported(String),
#[error("Transcript revision conflict: expected {expected}, actual {actual}")]
TranscriptRevisionConflict { expected: String, actual: String },
#[error("Internal error: {0}")]
Internal(String),
}
pub type AuthOAuthFlowSnapshotUpdate<'a> =
dyn FnMut(Option<&[u8]>) -> Result<Vec<u8>, RuntimeStoreError> + 'a;
#[derive(Debug, Clone)]
pub struct SessionDelta {
pub session_snapshot: Vec<u8>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MachineLifecycleCommit {
runtime_state: RuntimeState,
}
impl MachineLifecycleCommit {
pub(crate) fn new(runtime_state: RuntimeState) -> Self {
Self { runtime_state }
}
pub fn runtime_state(self) -> RuntimeState {
self.runtime_state
}
}
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
pub trait RuntimeStore: Send + Sync {
fn auth_authority_key(&self) -> Option<String> {
None
}
fn persist_auth_oauth_flow_snapshot(
&self,
snapshot_json: &[u8],
) -> Result<(), RuntimeStoreError> {
let _ = snapshot_json;
Err(RuntimeStoreError::Unsupported(
"persist_auth_oauth_flow_snapshot".into(),
))
}
fn load_auth_oauth_flow_snapshot(&self) -> Result<Option<Vec<u8>>, RuntimeStoreError> {
Err(RuntimeStoreError::Unsupported(
"load_auth_oauth_flow_snapshot".into(),
))
}
fn update_auth_oauth_flow_snapshot(
&self,
_update: &mut AuthOAuthFlowSnapshotUpdate<'_>,
) -> Result<(), RuntimeStoreError> {
Err(RuntimeStoreError::Unsupported(
"update_auth_oauth_flow_snapshot".into(),
))
}
async fn commit_session_snapshot(
&self,
runtime_id: &LogicalRuntimeId,
session_delta: SessionDelta,
) -> Result<(), RuntimeStoreError>;
async fn commit_session_transcript_rewrite_snapshot(
&self,
runtime_id: &LogicalRuntimeId,
session_delta: SessionDelta,
commit: &meerkat_core::TranscriptRewriteCommit,
) -> Result<(), RuntimeStoreError> {
let _ = (runtime_id, session_delta, commit);
Err(RuntimeStoreError::Unsupported(
"commit_session_transcript_rewrite_snapshot".into(),
))
}
async fn atomic_apply(
&self,
runtime_id: &LogicalRuntimeId,
session_delta: Option<SessionDelta>,
receipt: RunBoundaryReceipt,
input_updates: Vec<StoredInputState>,
session_store_key: Option<meerkat_core::types::SessionId>,
) -> Result<(), RuntimeStoreError>;
async fn load_input_states(
&self,
runtime_id: &LogicalRuntimeId,
) -> Result<Vec<StoredInputState>, RuntimeStoreError>;
async fn load_boundary_receipt(
&self,
runtime_id: &LogicalRuntimeId,
run_id: &RunId,
sequence: u64,
) -> Result<Option<RunBoundaryReceipt>, RuntimeStoreError>;
async fn load_session_snapshot(
&self,
runtime_id: &LogicalRuntimeId,
) -> Result<Option<Vec<u8>>, RuntimeStoreError>;
async fn clear_session_snapshot(
&self,
runtime_id: &LogicalRuntimeId,
) -> Result<(), RuntimeStoreError>;
async fn replace_session_snapshot_if_current(
&self,
runtime_id: &LogicalRuntimeId,
expected_current: &[u8],
replacement: Vec<u8>,
) -> Result<bool, RuntimeStoreError>;
async fn clear_session_snapshot_if_current(
&self,
runtime_id: &LogicalRuntimeId,
expected_current: &[u8],
) -> Result<bool, RuntimeStoreError>;
async fn persist_input_state(
&self,
runtime_id: &LogicalRuntimeId,
state: &StoredInputState,
) -> Result<(), RuntimeStoreError>;
async fn load_input_state(
&self,
runtime_id: &LogicalRuntimeId,
input_id: &InputId,
) -> Result<Option<StoredInputState>, RuntimeStoreError>;
async fn load_runtime_state(
&self,
runtime_id: &LogicalRuntimeId,
) -> Result<Option<RuntimeState>, RuntimeStoreError>;
async fn commit_machine_lifecycle(
&self,
runtime_id: &LogicalRuntimeId,
commit: MachineLifecycleCommit,
input_states: &[StoredInputState],
) -> Result<(), RuntimeStoreError>;
async fn persist_ops_lifecycle(
&self,
runtime_id: &LogicalRuntimeId,
snapshot: &crate::ops_lifecycle::PersistedOpsSnapshot,
) -> Result<(), RuntimeStoreError> {
let _ = (runtime_id, snapshot);
Err(RuntimeStoreError::Unsupported(
"persist_ops_lifecycle".into(),
))
}
async fn load_ops_lifecycle(
&self,
runtime_id: &LogicalRuntimeId,
) -> Result<Option<crate::ops_lifecycle::PersistedOpsSnapshot>, RuntimeStoreError> {
let _ = runtime_id;
Err(RuntimeStoreError::Unsupported("load_ops_lifecycle".into()))
}
}
pub use memory::InMemoryRuntimeStore;
#[cfg(feature = "sqlite-store")]
pub use sqlite::SqliteRuntimeStore;