use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use everruns_provider::typed_id::SessionId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SandboxCheckpointKind {
ProviderNative,
PortableWorkspace,
}
impl SandboxCheckpointKind {
pub const fn as_str(self) -> &'static str {
match self {
Self::ProviderNative => "provider_native",
Self::PortableWorkspace => "portable_workspace",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SandboxRef {
pub id: Uuid,
pub generation: i64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SandboxCheckpoint {
pub id: Uuid,
pub sandbox_id: Uuid,
pub generation: i64,
pub source_turn_id: Option<String>,
pub source_tool_call_id: Option<String>,
pub kind: SandboxCheckpointKind,
pub provider_ref: Option<String>,
pub workspace_revision: String,
pub attached_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NewSandboxCheckpoint {
pub sandbox_id: Uuid,
pub generation: i64,
pub source_turn_id: Option<String>,
pub source_tool_call_id: Option<String>,
pub kind: SandboxCheckpointKind,
pub provider_ref: Option<String>,
pub workspace_revision: String,
}
#[derive(Debug, thiserror::Error)]
pub enum SandboxCheckpointError {
#[error("sandbox checkpoint storage error: {0}")]
Storage(String),
#[error("sandbox {sandbox_id} is at generation {current}, write carried {carried}")]
StaleGeneration {
sandbox_id: Uuid,
current: i64,
carried: i64,
},
}
pub const MAX_CHECKPOINT_COLLECT_LIMIT: i64 = 1000;
#[async_trait]
pub trait SandboxCheckpointStore: Send + Sync {
async fn ensure_sandbox(
&self,
session_id: SessionId,
provider: &str,
) -> Result<SandboxRef, SandboxCheckpointError>;
async fn record_checkpoint(
&self,
checkpoint: NewSandboxCheckpoint,
) -> Result<SandboxCheckpoint, SandboxCheckpointError>;
async fn attach_checkpoint(
&self,
sandbox_id: Uuid,
checkpoint_id: Uuid,
generation: i64,
) -> Result<(), SandboxCheckpointError>;
async fn current_checkpoint(
&self,
sandbox_id: Uuid,
) -> Result<Option<SandboxCheckpoint>, SandboxCheckpointError>;
async fn rollback_current_checkpoint(
&self,
sandbox_id: Uuid,
checkpoint_id: Uuid,
generation: i64,
) -> Result<Option<SandboxCheckpoint>, SandboxCheckpointError>;
async fn collect_unattached_checkpoints(
&self,
sandbox_id: Uuid,
before: DateTime<Utc>,
limit: i64,
) -> Result<Vec<String>, SandboxCheckpointError>;
}
#[derive(Clone)]
pub struct SandboxCheckpointStoreExt(pub std::sync::Arc<dyn SandboxCheckpointStore>);
#[derive(Clone)]
pub struct DurableToolResultStoreExt(
pub std::sync::Arc<dyn everruns_core::durability::DurableToolResultStore>,
);