use super::AgentRunTime;
use crate::{
core::FailureKind, sandbox::Sandbox, state::AgentState, trajectory::TrajectoryEventKind,
};
use std::sync::Arc;
use tokio_util::sync::CancellationToken;
impl AgentRunTime {
pub(super) async fn open_sandbox(
&self,
ctx: &AgentState,
cancellation_token: CancellationToken,
) -> Result<Option<Arc<dyn Sandbox>>, String> {
tokio::select! {
_ = cancellation_token.cancelled() => Ok(None),
sandbox = self.sandbox.clone().open(&ctx.session_id) => {
sandbox.map(Some).map_err(|e| e.to_string())
}
}
}
pub(super) async fn persist_sandbox(&self, session_id: &str, sandbox: &Arc<dyn Sandbox>) {
if let Err(e) = sandbox.save().await {
self.emit(
session_id,
TrajectoryEventKind::Failed {
kind: FailureKind::SandboxSave.as_str().to_string(),
message: format!("best-effort sandbox save failed: {}", e),
},
)
.await;
}
}
}