use std::collections::BTreeMap;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
pub const SCHEMA_VERSION: u32 = 5;
pub const BUNDLE_SCHEMA_VERSION: u32 = 1;
pub const HARD_PATCH_BYTES: u64 = 64 * 1024 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum CapsuleState {
Creating,
Checkpointing,
Active,
Closed,
Integrating,
Integrated,
Dropping,
Orphaned,
Dropped,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Checkpoint {
pub commit: String,
pub message: String,
pub author_name: String,
pub author_email: String,
pub created_at_unix: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Evidence {
pub command: String,
pub exit_code: i32,
pub executed: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub output_sha256: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub output_bytes: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub summary: Option<String>,
pub patch_sha256: String,
pub recorded_at_unix: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ResultRef {
pub kind: ResultKind,
pub head_commit: String,
pub patch_sha256: String,
pub result_sha256: String,
pub patch_bytes: u64,
pub changed_paths: usize,
pub sealed_at_unix: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ResultKind {
NoChange,
Commit,
Patch,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Integration {
pub target_worktree: PathBuf,
pub target_git_dir: PathBuf,
pub target_head_ref: String,
pub target_head_before: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub target_head_after: Option<String>,
pub commit_message: String,
pub author_name: String,
pub author_email: String,
pub started_at_unix: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub integrated_at_unix: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CheckpointJournal {
pub head_before: String,
pub head_after: String,
pub patch_sha256: String,
pub message: String,
pub author_name: String,
pub author_email: String,
pub started_at_unix: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Cleanup {
#[serde(skip_serializing_if = "Option::is_none")]
pub branch_head: Option<String>,
pub require_sealed: bool,
pub started_at_unix: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Capsule {
pub schema_version: u32,
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub links: BTreeMap<String, String>,
pub state: CapsuleState,
pub source_worktree: PathBuf,
pub repository_common_dir: PathBuf,
#[serde(skip_serializing_if = "Option::is_none")]
pub workspace_git_dir: Option<PathBuf>,
pub workspace_path: PathBuf,
pub project_key: String,
pub branch: String,
pub base_commit: String,
pub created_at_unix: u64,
pub updated_at_unix: u64,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub checkpoints: Vec<Checkpoint>,
#[serde(skip_serializing_if = "Option::is_none")]
pub checkpoint: Option<CheckpointJournal>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub evidence: Vec<Evidence>,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<ResultRef>,
#[serde(skip_serializing_if = "Option::is_none")]
pub integration: Option<Integration>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cleanup: Option<Cleanup>,
#[serde(skip_serializing_if = "Option::is_none")]
pub closed_at_unix: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub dropped_at_unix: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CapsuleResult {
pub schema_version: u32,
pub capsule_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub links: BTreeMap<String, String>,
pub kind: ResultKind,
pub base_commit: String,
pub head_commit: String,
pub patch_sha256: String,
pub patch_bytes: u64,
pub changed_paths: Vec<GitPath>,
#[serde(default, skip_serializing_if = "is_zero")]
pub ignored_bytes: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ignored_content_sha256: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub ignored_paths: Vec<GitPath>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub checkpoints: Vec<Checkpoint>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub evidence: Vec<Evidence>,
pub created_at_unix: u64,
pub sealed_at_unix: u64,
}
#[allow(clippy::trivially_copy_pass_by_ref)]
const fn is_zero(value: &u64) -> bool {
*value == 0
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum CapsuleHealth {
Healthy,
MissingWorktree,
ForeignWorktree,
DriftedAfterClose,
IncompleteCreation,
IncompleteCheckpoint,
Dropped,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CapsuleStatus {
pub capsule: Capsule,
pub health: CapsuleHealth,
#[serde(skip_serializing_if = "Option::is_none")]
pub head_commit: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub dirty: Option<bool>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub changed_paths: Vec<GitPath>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub ignored_paths: Vec<GitPath>,
#[serde(skip_serializing_if = "Option::is_none")]
pub commits_ahead: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sealed: Option<bool>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CapsuleSummary {
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
pub state: CapsuleState,
pub workspace_path: PathBuf,
pub base_commit: String,
pub updated_at_unix: u64,
}
impl From<&Capsule> for CapsuleSummary {
fn from(capsule: &Capsule) -> Self {
Self {
id: capsule.id.clone(),
label: capsule.label.clone(),
state: capsule.state,
workspace_path: capsule.workspace_path.clone(),
base_commit: capsule.base_commit.clone(),
updated_at_unix: capsule.updated_at_unix,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RecoveryAction {
pub capsule_id: String,
pub previous_state: CapsuleState,
pub state: CapsuleState,
pub action: String,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum GitPath {
Utf8(String),
UnixBytes {
unix_bytes_hex: String,
},
}
impl<'de> Deserialize<'de> for GitPath {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(deny_unknown_fields, untagged)]
enum Repr {
Utf8(String),
UnixBytes { unix_bytes_hex: String },
}
let path = match Repr::deserialize(deserializer)? {
Repr::Utf8(value) => Self::Utf8(value),
Repr::UnixBytes { unix_bytes_hex } => Self::UnixBytes { unix_bytes_hex },
};
if path.is_valid_encoding() {
Ok(path)
} else {
Err(serde::de::Error::custom(
"Git path encoding must be non-empty, NUL-free, canonical lowercase hex, and raw-byte form must not encode valid UTF-8",
))
}
}
}
impl GitPath {
pub fn as_str(&self) -> Option<&str> {
match self {
Self::Utf8(value) => Some(value),
Self::UnixBytes { .. } => None,
}
}
pub fn is_valid_encoding(&self) -> bool {
match self {
Self::Utf8(value) => !value.is_empty() && !value.contains('\0'),
Self::UnixBytes { unix_bytes_hex } => {
!unix_bytes_hex.is_empty()
&& unix_bytes_hex
.bytes()
.all(|byte| byte.is_ascii_digit() || matches!(byte, b'a'..=b'f'))
&& hex::decode(unix_bytes_hex).is_ok_and(|bytes| {
!bytes.is_empty()
&& !bytes.contains(&0)
&& std::str::from_utf8(&bytes).is_err()
})
}
}
}
pub fn to_path_buf(&self) -> Option<PathBuf> {
match self {
Self::Utf8(value) => Some(PathBuf::from(value)),
Self::UnixBytes { unix_bytes_hex } => {
let bytes = hex::decode(unix_bytes_hex).ok()?;
#[cfg(unix)]
{
use std::os::unix::ffi::OsStringExt;
Some(PathBuf::from(std::ffi::OsString::from_vec(bytes)))
}
#[cfg(not(unix))]
{
let _ = bytes;
None
}
}
}
}
}
impl PartialEq<str> for GitPath {
fn eq(&self, other: &str) -> bool {
self.as_str() == Some(other)
}
}
impl PartialEq<&str> for GitPath {
fn eq(&self, other: &&str) -> bool {
self == *other
}
}
impl std::fmt::Display for GitPath {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Utf8(value) => formatter.write_str(value),
Self::UnixBytes { unix_bytes_hex } => write!(formatter, "unix-bytes:{unix_bytes_hex}"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ArtifactKind {
ResultManifest,
ResultPatch,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ArtifactDescriptor {
pub kind: ArtifactKind,
pub name: String,
pub media_type: String,
pub uri: String,
pub content_address: String,
pub sha256: String,
pub bytes: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ArtifactBundle {
pub schema_version: u32,
pub capsule_id: String,
pub artifacts: Vec<ArtifactDescriptor>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ExportReport {
pub bundle: ArtifactBundle,
pub output_directory: PathBuf,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct UnreadableRecord {
pub id: String,
pub error: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CapsuleListing {
pub capsules: Vec<CapsuleSummary>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub unreadable: Vec<UnreadableRecord>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct VerificationReport {
pub bundle_directory: PathBuf,
pub capsule_id: String,
pub kind: ResultKind,
pub base_commit: String,
pub head_commit: String,
pub patch_bytes: u64,
pub patch_sha256: String,
pub changed_paths: usize,
pub evidence_total: usize,
pub evidence_executed: usize,
pub evidence_failed: usize,
pub repository_checked: bool,
pub signature_authenticated: bool,
}