use std::collections::BTreeSet;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use anyhow::{Context, Result, bail, ensure};
use crate::checkpoint_transfer::{
CheckpointTransfer, capture_stdin_command, export_stdin_command, pack_stdin_command,
};
use crate::session_manager::{
ManagedSessionHandle, ManagedSessionLease, SessionManagerControl, StandaloneSession,
new_command_id, worker_connect_needs_restart,
};
use crate::worker_client::RelayRejected;
use mj_checkpoint::archive::{
BundleManifest, CanonicalSessionSnapshot, SessionManifest, TargetManifest,
verify_archive_streaming,
};
use mj_checkpoint::checkpoint::{
CHECKPOINT_EXPORT_PROTOCOL_VERSION, CHECKPOINT_STAGING_PROTOCOL_VERSION, CapturedCheckpoint,
CheckpointCaptureSpec, CheckpointExportSpec, CheckpointPackSpec, CheckpointRepositoryCapture,
CheckpointRepositorySpec, canonical_session_contains_prompt, checkpoint_sha256,
};
use mj_core::config::{HarnessKind, sessions_dir};
use mj_core::state::{
CheckpointMetadata, ManagedSessionSnapshot, SessionRecord, SessionState, State,
};
use mj_transcript::projection::canonical_session_from_materialized;
use crate::targets::{
self, CommandExecutor, CommandOutput, CommandSpec, ProcessExecutor, ProvisionStage,
ProvisionStageGuard,
};
use mj_core::relay::{RelayCommand, RelayCursor, RelayExecutionState};
use super::backend::backend_locator;
use super::readiness::wait_for_native_session_in_stage;
use super::worker_restart::{InstalledWorkerRestart, RESTART_FOR_CHECKPOINT};
use super::{
Controller, execute_checked, now, persist_session_record_transition_or_restore,
target_profile_home,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SessionExportLayout {
pub backend: targets::TargetLocator,
pub workspace_root: String,
pub primary_repository: String,
pub repositories: Vec<CheckpointRepositorySpec>,
pub managed_worktree: Option<mj_core::state::ManagedWorktree>,
}
const CHECKPOINT_BARRIER_TIMEOUT: Duration = Duration::from_secs(30);
const CHECKPOINT_CANCEL_TIMEOUT: Duration = Duration::from_secs(30);
const CHECKPOINT_BARRIER_TIMEOUT_AFTER_RESTART: Duration = Duration::from_secs(300);
mod archives;
pub use archives::*;
mod lease;
pub use lease::*;
mod barrier;
mod capture;
mod latched;
mod layout;
mod persist;
mod relay;
pub use barrier::*;
mod workspace_lease;
pub use workspace_lease::*;
mod validate;
use validate::*;
mod staging;
pub(super) use staging::*;
#[cfg(test)]
mod tests;