use serde::{Deserialize, Deserializer, Serialize};
use crate::{
hash_bytes, ChangeType, Commit, FileChange, FileMode, Hash, Tree, TreeEntry, TreeEntryKind,
};
pub const STAGED_ENVELOPE_MAX_COMMITS: usize = 500;
pub const STAGED_OPERATION_MAX_COMMITS: usize = 100_000;
pub const STAGED_MAX_TREE_OBJECTS: usize = 100_000;
pub const STAGED_MAX_DIRECT_TREE_ENTRIES: usize = 1_000_000;
pub const STAGED_MAX_RESOLVED_MANIFEST_ENTRIES: usize = 1_000_000;
pub const STAGED_MAX_FILE_CHANGES: usize = 250_000;
pub const STAGED_MAX_CANONICAL_METADATA_BYTES: usize = 64 * 1024 * 1024;
pub const STAGED_MAX_EXPANDED_PATH_BYTES: usize = 64 * 1024 * 1024;
pub const STAGED_MAX_CHUNK_REFS: usize = 1_000_000;
pub const STAGED_MAX_BLOBS: usize = 50_000;
pub const STAGED_MAX_DECLARED_BLOB_BYTES: u64 = 25 * 1024 * 1024 * 1024;
pub const STAGED_WINDOW_BUDGET_PROTOCOL: &str = "window_v1";
pub const STAGED_WINDOW_MAX_RESOLVED_MANIFEST_ENTRIES: usize = 16_000_000;
pub const STAGED_WINDOW_MAX_BLOBS: usize = 250_000;
pub const STAGED_ABORT_PROTOCOL: &str = "v1";
pub const KNOWN_LOSS_PROTOCOL: &str = "report_v1";
pub const RETENTION_PROTOCOL: &str = "expired_v1";
pub const OPERATOR_LOSS_REASON: &str = "operator_adjudicated_loss";
pub const RETENTION_EXPIRED_REASON: &str = "retention_expired";
pub const MAPPING_PROOF_PROTOCOL: &str = "async_v1";
pub const ORDINARY_BOOTSTRAP_PROTOCOL: &str = "headless_preload_v1";
pub const MAPPING_PROOF_MAX_BLOBS: usize = 128;
pub const MAPPING_PROOF_MAX_SET_CHUNK_REFS: usize = 131_072;
pub const MAPPING_PROOF_MAX_BLOB_CHUNK_REFS: usize = 131_072;
pub const MAPPING_PROOF_MAX_SET_BYTES: u64 = 25 * 1024 * 1024 * 1024;
pub const MAPPING_PROOF_MAX_BLOB_BYTES: u64 = 25 * 1024 * 1024 * 1024;
pub const MAPPING_PROOF_MAX_CHUNK_BYTES: u64 = 64 * 1024 * 1024;
pub const MAPPING_PROOF_PAGE_CHUNK_REFS: usize = 10_000;
pub const MAPPING_PROOF_PAGE_BODY_BYTES: usize = 2 * 1024 * 1024;
pub const MAPPING_PROOF_GENERATION_CONFLICT: &str = "mapping_generation_conflict";
pub fn staged_branch_metadata_bytes(branch: &BranchPushData) -> usize {
branch
.name
.len()
.saturating_add(branch.description.as_ref().map_or(0, String::len))
.saturating_add(branch.parent_branch.as_ref().map_or(0, String::len))
.saturating_add(branch.created_at.len())
.saturating_add(branch.status.len())
.saturating_add(branch.close_reason.as_ref().map_or(0, String::len))
}
pub fn staged_commit_metadata_bytes(commit: &CommitData) -> usize {
commit
.hash
.len()
.saturating_add(commit.branch_name.len())
.saturating_add(commit.manifest_hash.len())
.saturating_add(commit.author.len())
.saturating_add(commit.message.as_ref().map_or(0, String::len))
.saturating_add(commit.files.iter().fold(0usize, |total, file| {
total
.saturating_add(file.path.len())
.saturating_add(file.old_path.as_ref().map_or(0, String::len))
.saturating_add(160)
}))
}
pub fn staged_tree_metadata_bytes(tree: &TreeData) -> usize {
tree.entries.iter().fold(0usize, |total, entry| {
total
.saturating_add(entry.name.len())
.saturating_add(entry.hash.len())
.saturating_add(16)
})
}
#[derive(Serialize, Deserialize)]
pub struct RepoResponse {
pub name: String,
pub description: Option<String>,
pub head: Option<String>,
pub is_public: bool,
pub owner: Option<String>,
pub emoji: Option<String>,
pub updated_at: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct RepoListResponse {
pub repos: Vec<RepoResponse>,
}
#[derive(Serialize, Deserialize)]
pub struct CreateRepoRequest {
pub name: String,
pub description: Option<String>,
#[serde(default)]
pub is_public: bool,
#[serde(default)]
pub organization_slug: Option<String>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct CommitData {
pub hash: String,
pub branch_name: String,
pub parent_hash: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub merge_parent_hash: Option<String>,
pub manifest_hash: String,
pub author: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
pub timestamp: String,
pub files: Vec<FileChangeData>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct FileChangeData {
pub path: String,
pub change_type: String,
pub old_blob_hash: Option<String>,
pub new_blob_hash: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub old_path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub old_mode: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub new_mode: Option<String>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct ChunkRefData {
pub hash: String,
pub offset: u64,
pub size: u32,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct BlobData {
pub hash: String,
pub content: Vec<u8>,
pub size: u64,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub chunks: Vec<ChunkRefData>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mapping_proof_token: Option<String>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct TreeData {
pub hash: String,
pub entries: Vec<TreeEntryData>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct TreeEntryData {
pub name: String,
pub kind: String,
pub hash: String,
pub mode: String,
}
#[derive(Serialize, Deserialize)]
pub struct PushRequest {
pub expected_head: Option<String>,
#[serde(default)]
pub expected_branch_head: Option<String>,
#[serde(default)]
pub force: bool,
pub branch: Option<BranchPushData>,
pub commits: Vec<CommitData>,
pub blobs: Vec<BlobData>,
#[serde(default)]
pub trees: Vec<TreeData>,
}
#[derive(Serialize, Deserialize)]
pub struct StagedPushRequest {
pub stage_id: String,
#[serde(deserialize_with = "deserialize_required_optional_string")]
pub expected_branch_head: Option<String>,
pub branch: BranchPushData,
pub finalize: bool,
#[serde(default)]
pub force: bool,
pub target_head: Option<String>,
pub commits: Vec<CommitData>,
pub blobs: Vec<BlobData>,
#[serde(default)]
pub trees: Vec<TreeData>,
}
#[derive(Serialize, Deserialize)]
pub struct StagedAbortRequest {
pub branch_name: String,
#[serde(deserialize_with = "deserialize_required_optional_string")]
pub expected_branch_head: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct StagedAbortResponse {
pub aborted: bool,
pub state: String,
}
fn deserialize_required_optional_string<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
Option::<String>::deserialize(deserializer)
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct BranchPushData {
pub name: String,
pub description: Option<String>,
pub parent_branch: Option<String>,
pub status: String,
pub created_at: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub close_reason: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct PushResponse {
pub success: bool,
pub new_head: Option<String>,
pub message: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub view: Option<ViewResponseData>,
}
#[derive(Serialize, Deserialize)]
pub struct PullQuery {
pub since: Option<String>,
pub branch_name: Option<String>,
#[serde(default)]
pub force: bool,
#[serde(default)]
pub since_rename_id: i64,
pub depth: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub known_loss_protocol: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub retention_protocol: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct BranchPullData {
pub name: String,
pub description: Option<String>,
pub parent_branch: Option<String>,
pub status: String,
pub created_at: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub close_reason: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct BranchRenameData {
pub id: i64,
pub old_name: String,
pub new_name: String,
pub renamed_at: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct MissingContentData {
pub kind: String,
pub hash: String,
pub reason_code: String,
}
#[derive(Serialize, Deserialize)]
pub struct PullResponse {
pub head: Option<String>,
pub branch: Option<BranchPullData>,
pub branches: Vec<BranchPullData>,
pub commits: Vec<CommitData>,
pub blobs: Vec<BlobData>,
pub trees: Vec<TreeData>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub renames: Vec<BranchRenameData>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub restricted_blobs: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub missing_content: Vec<MissingContentData>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<TagData>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub view: Option<ViewResponseData>,
}
pub const RELEASE_PROTOCOL_V1: &str = "v1";
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct TagData {
pub name: String,
pub commit: String,
#[serde(default)]
pub title: Option<String>,
#[serde(default)]
pub notes: Option<String>,
#[serde(default)]
pub prerelease: bool,
#[serde(default)]
pub finalized: bool,
#[serde(default)]
pub created_by: String,
pub created_at: String,
#[serde(default)]
pub published_at: Option<String>,
#[serde(default)]
pub revision: i64,
#[serde(default)]
pub assets: Vec<TagAssetData>,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct TagAssetData {
pub name: String,
pub blob_hash: String,
pub size: u64,
#[serde(default = "default_asset_content_type")]
pub content_type: String,
}
fn default_asset_content_type() -> String {
"application/octet-stream".to_string()
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ViewQuery {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub view_protocol: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub view: Option<String>,
}
impl ViewQuery {
pub fn new(view: Option<&crate::ViewSpec>) -> Self {
ViewQuery {
view_protocol: Some(crate::view::VIEW_PROTOCOL.to_string()),
view: view.map(|v| v.to_canonical()),
}
}
pub fn is_view_request(&self) -> bool {
self.view_protocol.as_deref() == Some(crate::view::VIEW_PROTOCOL)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ViewCommitMapping {
pub full_hash: String,
pub view_hash: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ViewResponseData {
pub effective_view: String,
pub view_id: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub commits: Vec<ViewCommitMapping>,
#[serde(default, skip_serializing_if = "is_zero")]
pub excluded_by_permission: u32,
}
fn is_zero(value: &u32) -> bool {
*value == 0
}
#[derive(Serialize, Deserialize)]
pub struct BlobCheckRequest {
pub hashes: Vec<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub require_verified_receipts: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub verify_content: bool,
}
#[derive(Serialize, Deserialize)]
pub struct BlobCheckResponse {
pub missing: Vec<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub verified_content: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub verified_receipts_required: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlobProofRequest {
pub blobs: Vec<BlobProofDescriptor>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlobProofDescriptor {
pub hash: String,
pub size: u64,
pub mapping_digest: String,
pub total_chunks: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlobProofChunk {
pub hash: String,
pub offset: u64,
pub size: u32,
}
pub fn blob_mapping_digest(chunks: &[BlobProofChunk]) -> String {
let mut canonical = Vec::with_capacity(chunks.len().saturating_mul(84));
for (index, chunk) in chunks.iter().enumerate() {
canonical.extend_from_slice(&(index as u64).to_be_bytes());
canonical.extend_from_slice(&chunk.offset.to_be_bytes());
canonical.extend_from_slice(&chunk.size.to_be_bytes());
canonical.extend_from_slice(chunk.hash.as_bytes());
}
hash_bytes(&canonical).to_string()
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlobProofPagesRequest {
pub pages: Vec<BlobProofMappingPage>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlobProofMappingPage {
pub blob_index: u32,
pub first_chunk_index: u32,
pub chunks: Vec<BlobProofChunk>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlobProofPagesResponse {
pub accepted_chunks: u32,
pub complete_blobs: Vec<u32>,
pub all_mappings_complete: bool,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlobProofFinalizeRequest {}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlobProofResponse {
pub verified: Vec<String>,
pub missing: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub proof_token: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mapping_proof_job: Option<MappingProofJob>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MappingProofJob {
pub token: String,
pub status: String,
pub retry_after_ms: u64,
}
fn is_false(value: &bool) -> bool {
!*value
}
#[derive(Serialize, Deserialize)]
pub struct BlobInfoRequest {
pub hashes: Vec<String>,
}
#[derive(Serialize, Deserialize)]
pub struct BlobInfoResponse {
pub blobs: Vec<BlobData>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub restricted: Vec<String>,
}
#[derive(Serialize, Deserialize)]
pub struct CommitInfoRequest {
pub hashes: Vec<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub metadata_only: bool,
}
#[derive(Serialize, Deserialize)]
pub struct CommitInfoResponse {
pub commits: Vec<CommitData>,
pub trees: Vec<TreeData>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub view: Option<ViewResponseData>,
}
pub const CHUNK_BATCH_PROTOCOL: &str = "bounded_v1";
pub const CHUNK_BATCH_MAX_HASHES: usize = 10_000;
#[derive(Serialize, Deserialize)]
pub struct ChunkCheckRequest {
pub hashes: Vec<String>,
#[serde(default)]
pub sizes: Option<Vec<u64>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub chunk_batch_protocol: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct ChunkUploadInfo {
pub hash: String,
pub upload_url: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct ChunkCheckServerResponse {
pub missing: Vec<ChunkUploadInfo>,
}
#[derive(Serialize, Deserialize)]
pub struct ChunkDownloadRequest {
pub hashes: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub chunk_batch_protocol: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct ChunkDownloadInfo {
pub hash: String,
pub download_url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub content: Option<Vec<u8>>,
}
#[derive(Serialize, Deserialize)]
pub struct ChunkDownloadResponse {
pub chunks: Vec<ChunkDownloadInfo>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub batch_url: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub restricted: Vec<String>,
}
#[derive(Serialize, Deserialize)]
pub struct ErrorResponse {
pub error: String,
}
pub fn tree_data_to_core(td: &TreeData) -> Result<Tree, String> {
let tree_hash = Hash::from_hex(&td.hash).map_err(|e| e.to_string())?;
let mut entries = Vec::with_capacity(td.entries.len());
for e in &td.entries {
crate::tree::validate_entry_name(&e.name).map_err(|err| err.to_string())?;
let kind = match e.kind.as_str() {
"tree" => TreeEntryKind::Tree,
"blob" => TreeEntryKind::Blob,
other => return Err(format!("invalid tree entry kind: {other}")),
};
let mode = match (kind, e.mode.as_str()) {
(TreeEntryKind::Tree, "tree") => FileMode::Regular,
(TreeEntryKind::Blob, "regular") => FileMode::Regular,
(TreeEntryKind::Blob, "executable") => FileMode::Executable,
(TreeEntryKind::Blob, "symlink") => FileMode::Symlink,
_ => {
return Err(format!(
"invalid tree entry mode {:?} for kind {}",
e.mode,
e.kind.as_str()
))
}
};
entries.push(TreeEntry {
name: e.name.clone(),
kind,
hash: Hash::from_hex(&e.hash).map_err(|err| err.to_string())?,
mode,
});
}
let tree = Tree::new(entries).map_err(|e| e.to_string())?;
if tree.hash != tree_hash {
return Err(format!(
"tree hash mismatch: peer claimed {}, content hashes to {}",
td.hash, tree.hash
));
}
Ok(tree)
}
fn wire_hash(field: &str, value: &str) -> Result<Hash, String> {
Hash::from_hex(value).map_err(|e| format!("invalid {field} hash {value:?}: {e}"))
}
fn wire_optional_hash(field: &str, value: Option<&String>) -> Result<Option<Hash>, String> {
value.map(|s| wire_hash(field, s)).transpose()
}
fn file_change_data_to_core(f: &FileChangeData, index: usize) -> Result<FileChange, String> {
crate::tree::validate_tree_path(&f.path)
.map_err(|e| format!("invalid file change #{index} path: {e}"))?;
if let Some(old_path) = &f.old_path {
crate::tree::validate_tree_path(old_path)
.map_err(|e| format!("invalid file change #{index} old_path: {e}"))?;
}
let change_type = match f.change_type.as_str() {
"added" => ChangeType::Added,
"modified" => ChangeType::Modified,
"deleted" => ChangeType::Deleted,
"renamed" => ChangeType::Renamed,
other => return Err(format!("invalid file change #{index} type: {other}")),
};
let parse_mode = |field: &str, mode: Option<&String>| {
mode.map(|mode| match mode.as_str() {
"regular" => Ok(FileMode::Regular),
"executable" => Ok(FileMode::Executable),
"symlink" => Ok(FileMode::Symlink),
other => Err(format!("invalid file change #{index} {field}: {other}")),
})
.transpose()
};
Ok(FileChange {
path: f.path.clone(),
change_type,
old_blob_hash: wire_optional_hash("old blob", f.old_blob_hash.as_ref())?,
new_blob_hash: wire_optional_hash("new blob", f.new_blob_hash.as_ref())?,
old_path: f.old_path.clone(),
old_mode: parse_mode("old_mode", f.old_mode.as_ref())?,
new_mode: parse_mode("new_mode", f.new_mode.as_ref())?,
})
}
pub fn file_mode_to_wire(mode: FileMode) -> &'static str {
match mode {
FileMode::Regular => "regular",
FileMode::Executable => "executable",
FileMode::Symlink => "symlink",
}
}
pub fn commit_data_to_core(cd: &CommitData) -> Result<Commit, String> {
let claimed_hash = wire_hash("commit", &cd.hash)?;
let parent_hash = wire_optional_hash("parent", cd.parent_hash.as_ref())?;
let merge_parent_hash = wire_optional_hash("merge parent", cd.merge_parent_hash.as_ref())?;
let manifest_hash = wire_hash("manifest", &cd.manifest_hash)?;
let timestamp = chrono::DateTime::parse_from_rfc3339(&cd.timestamp)
.map_err(|e| format!("invalid commit timestamp {:?}: {e}", cd.timestamp))?
.with_timezone(&chrono::Utc);
let files: Vec<FileChange> = cd
.files
.iter()
.enumerate()
.map(|(i, f)| file_change_data_to_core(f, i))
.collect::<Result<_, _>>()?;
Commit::rehydrate_verified(
&claimed_hash,
cd.branch_name.clone(),
parent_hash,
merge_parent_hash,
manifest_hash,
cd.author.clone(),
cd.message.clone(),
files,
timestamp,
)
.map_err(|e| e.to_string())
}
pub fn tree_to_wire(tree: &Tree) -> TreeData {
TreeData {
hash: tree.hash.to_string(),
entries: tree
.entries
.iter()
.map(|e| TreeEntryData {
name: e.name.clone(),
kind: e.kind.as_str().to_string(),
hash: e.hash.to_string(),
mode: match (e.kind, e.mode) {
(TreeEntryKind::Tree, _) => "tree".to_string(),
(TreeEntryKind::Blob, FileMode::Regular) => "regular".to_string(),
(TreeEntryKind::Blob, FileMode::Executable) => "executable".to_string(),
(TreeEntryKind::Blob, FileMode::Symlink) => "symlink".to_string(),
},
})
.collect(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn view_query_is_opt_in_and_url_encodable() {
let view = crate::ViewSpec::new(["apps/web"], ["apps/web/config"], ["AGENTS.md"]).unwrap();
let query = ViewQuery::new(Some(&view));
assert!(query.is_view_request());
assert_eq!(
serde_json::to_value(&query).unwrap(),
serde_json::json!({
"view_protocol": "view_v1",
"view": "+apps/web\n-apps/web/config\n=AGENTS.md",
})
);
let parsed = crate::ViewSpec::parse(query.view.as_deref().unwrap()).unwrap();
assert_eq!(parsed, view);
assert_eq!(
serde_json::to_value(ViewQuery::default()).unwrap(),
serde_json::json!({})
);
assert!(!ViewQuery::default().is_view_request());
}
#[test]
fn responses_without_view_data_still_parse_and_omit_the_field() {
let pull: PullResponse = serde_json::from_value(serde_json::json!({
"head": null, "branch": null, "branches": [], "commits": [], "blobs": [], "trees": [],
}))
.unwrap();
assert!(pull.view.is_none());
assert!(serde_json::to_value(&pull).unwrap().get("view").is_none());
let push: PushResponse = serde_json::from_value(serde_json::json!({
"success": true, "new_head": "h", "message": "ok",
}))
.unwrap();
assert!(push.view.is_none());
let info = CommitInfoResponse {
commits: vec![],
trees: vec![],
view: Some(ViewResponseData {
effective_view: "+apps/web".to_string(),
view_id: "id".to_string(),
commits: vec![ViewCommitMapping {
full_hash: "f".to_string(),
view_hash: "v".to_string(),
}],
excluded_by_permission: 0,
}),
};
assert_eq!(
serde_json::to_value(&info).unwrap()["view"],
serde_json::json!({
"effective_view": "+apps/web",
"view_id": "id",
"commits": [{ "full_hash": "f", "view_hash": "v" }],
})
);
}
#[test]
fn async_mapping_proof_wire_is_versioned_and_exact() {
let request = BlobProofRequest {
blobs: vec![BlobProofDescriptor {
hash: "a".repeat(64),
size: 3,
mapping_digest: "6ed22faf011636d53e22b7f48e5c9ac2ef718887ebc0b7009ddb08476fdced82"
.to_string(),
total_chunks: 1,
}],
};
assert_eq!(MAPPING_PROOF_PROTOCOL, "async_v1");
assert_eq!(
serde_json::to_value(&request).unwrap(),
serde_json::json!({
"blobs": [{
"hash": "a".repeat(64),
"size": 3,
"mapping_digest": "6ed22faf011636d53e22b7f48e5c9ac2ef718887ebc0b7009ddb08476fdced82",
"total_chunks": 1
}]
})
);
assert_eq!(
serde_json::to_string(&request).unwrap(),
format!(
"{{\"blobs\":[{{\"hash\":\"{}\",\"size\":3,\"mapping_digest\":\"6ed22faf011636d53e22b7f48e5c9ac2ef718887ebc0b7009ddb08476fdced82\",\"total_chunks\":1}}]}}",
"a".repeat(64)
)
);
let created: BlobProofResponse = serde_json::from_value(serde_json::json!({
"verified": [],
"missing": [],
"mapping_proof_job": {
"token": "proof-token",
"status": "uploading",
"retry_after_ms": 500
}
}))
.unwrap();
assert_eq!(
created.mapping_proof_job.as_ref().unwrap().token,
"proof-token"
);
assert_eq!(
serde_json::to_string(&created).unwrap(),
"{\"verified\":[],\"missing\":[],\"mapping_proof_job\":{\"token\":\"proof-token\",\"status\":\"uploading\",\"retry_after_ms\":500}}"
);
let pages = BlobProofPagesRequest {
pages: vec![BlobProofMappingPage {
blob_index: 0,
first_chunk_index: 0,
chunks: vec![BlobProofChunk {
hash: "c".repeat(64),
offset: 0,
size: 3,
}],
}],
};
assert_eq!(
serde_json::to_value(&pages).unwrap(),
serde_json::json!({
"pages": [{
"blob_index": 0,
"first_chunk_index": 0,
"chunks": [{
"hash": "c".repeat(64),
"offset": 0,
"size": 3
}]
}]
})
);
assert_eq!(
serde_json::to_string(&pages).unwrap(),
format!(
"{{\"pages\":[{{\"blob_index\":0,\"first_chunk_index\":0,\"chunks\":[{{\"hash\":\"{}\",\"offset\":0,\"size\":3}}]}}]}}",
"c".repeat(64)
)
);
let accepted: BlobProofPagesResponse = serde_json::from_value(serde_json::json!({
"accepted_chunks": 1,
"complete_blobs": [0],
"all_mappings_complete": true
}))
.unwrap();
assert!(accepted.all_mappings_complete);
assert_eq!(
serde_json::to_string(&accepted).unwrap(),
"{\"accepted_chunks\":1,\"complete_blobs\":[0],\"all_mappings_complete\":true}"
);
assert_eq!(
serde_json::to_value(BlobProofFinalizeRequest {}).unwrap(),
serde_json::json!({})
);
let finalized: BlobProofResponse = serde_json::from_value(serde_json::json!({
"verified": [],
"missing": [],
"mapping_proof_job": {
"token": "proof-token",
"status": "pending",
"retry_after_ms": 500
}
}))
.unwrap();
assert_eq!(
finalized.mapping_proof_job.as_ref().unwrap().status,
"pending"
);
assert_eq!(
serde_json::to_string(&finalized).unwrap(),
"{\"verified\":[],\"missing\":[],\"mapping_proof_job\":{\"token\":\"proof-token\",\"status\":\"pending\",\"retry_after_ms\":500}}"
);
let terminal = serde_json::to_value(BlobProofResponse {
verified: vec!["a".repeat(64)],
missing: Vec::new(),
proof_token: Some("proof-token".to_string()),
mapping_proof_job: None,
})
.unwrap();
assert_eq!(terminal["proof_token"], "proof-token");
assert_eq!(
serde_json::to_string(&BlobProofResponse {
verified: vec!["a".repeat(64)],
missing: Vec::new(),
proof_token: Some("proof-token".to_string()),
mapping_proof_job: None,
})
.unwrap(),
format!(
"{{\"verified\":[\"{}\"],\"missing\":[],\"proof_token\":\"proof-token\"}}",
"a".repeat(64)
)
);
}
#[test]
fn mapping_digest_matches_the_stored_receipt_identity() {
let chunks = vec![
BlobProofChunk {
hash: "c".repeat(64),
offset: 0,
size: 3,
},
BlobProofChunk {
hash: "c".repeat(64),
offset: 3,
size: 3,
},
];
assert_eq!(
blob_mapping_digest(&chunks[..1]),
"6ed22faf011636d53e22b7f48e5c9ac2ef718887ebc0b7009ddb08476fdced82"
);
assert_eq!(
blob_mapping_digest(&chunks),
"2604417dcfdfa8bc4fc85d2b24a965fb8d7c328d00723c31b75ad7bbdaa194fe"
);
assert_eq!(
blob_mapping_digest(&[]),
"af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262"
);
}
#[test]
fn blob_check_verified_content_is_append_only_and_fail_closed() {
let legacy: BlobCheckResponse =
serde_json::from_value(serde_json::json!({ "missing": [] })).unwrap();
assert!(!legacy.verified_content);
assert!(!legacy.verified_receipts_required);
let ordinary = serde_json::to_value(BlobCheckRequest {
hashes: vec!["ab".repeat(32)],
require_verified_receipts: false,
verify_content: false,
})
.unwrap();
assert!(ordinary.get("verify_content").is_none());
assert!(ordinary.get("require_verified_receipts").is_none());
let receipt_strict = serde_json::to_value(BlobCheckRequest {
hashes: vec!["ab".repeat(32)],
require_verified_receipts: true,
verify_content: false,
})
.unwrap();
assert_eq!(receipt_strict["require_verified_receipts"], true);
assert!(receipt_strict.get("verify_content").is_none());
let verified = serde_json::to_value(BlobCheckRequest {
hashes: vec!["ab".repeat(32)],
require_verified_receipts: false,
verify_content: true,
})
.unwrap();
assert_eq!(verified["verify_content"], true);
}
#[test]
fn staged_push_protocol_is_structurally_separate_from_legacy_push() {
let ordinary: PushRequest = serde_json::from_value(serde_json::json!({
"expected_head": null,
"branch": null,
"commits": [],
"blobs": [],
"trees": []
}))
.unwrap();
let ordinary = serde_json::to_value(&ordinary).unwrap();
assert!(ordinary.get("finalize").is_none());
let staged = serde_json::to_value(StagedPushRequest {
stage_id: "0123456789abcdef0123456789abcdef".to_string(),
expected_branch_head: None,
branch: BranchPushData {
name: "main".to_string(),
description: None,
parent_branch: None,
status: "open".to_string(),
created_at: "2026-01-01T00:00:00Z".to_string(),
close_reason: None,
},
finalize: false,
force: true,
target_head: None,
commits: Vec::new(),
blobs: Vec::new(),
trees: Vec::new(),
})
.unwrap();
assert_eq!(staged["expected_branch_head"], serde_json::Value::Null);
assert_eq!(staged["finalize"], false);
assert_eq!(staged["force"], true);
assert!(staged["branch"].get("close_reason").is_none());
let legacy_branch: BranchPushData = serde_json::from_value(serde_json::json!({
"name": "main",
"description": null,
"parent_branch": null,
"status": "open",
"created_at": "2026-01-01T00:00:00Z"
}))
.unwrap();
assert_eq!(legacy_branch.close_reason, None);
let mut missing_boundary = staged.clone();
missing_boundary
.as_object_mut()
.unwrap()
.remove("expected_branch_head");
assert!(serde_json::from_value::<StagedPushRequest>(missing_boundary).is_err());
let legacy_staged: StagedPushRequest = serde_json::from_value(serde_json::json!({
"stage_id": "0123456789abcdef0123456789abcdef",
"expected_branch_head": null,
"branch": {
"name": "main",
"description": null,
"parent_branch": null,
"status": "open",
"created_at": "2026-01-01T00:00:00Z"
},
"finalize": false,
"target_head": null,
"commits": [],
"blobs": [],
"trees": []
}))
.unwrap();
assert!(
!legacy_staged.force,
"omitted force must remain fail-closed"
);
let ordinary_info = serde_json::to_value(CommitInfoRequest {
hashes: vec!["cd".repeat(32)],
metadata_only: false,
})
.unwrap();
assert!(ordinary_info.get("metadata_only").is_none());
let metadata_info = serde_json::to_value(CommitInfoRequest {
hashes: vec!["cd".repeat(32)],
metadata_only: true,
})
.unwrap();
assert_eq!(metadata_info["metadata_only"], true);
let abort = serde_json::to_value(StagedAbortRequest {
branch_name: "main".to_string(),
expected_branch_head: None,
})
.unwrap();
assert_eq!(abort["branch_name"], "main");
assert!(abort.get("expected_branch_head").is_some());
let response: StagedAbortResponse = serde_json::from_value(serde_json::json!({
"aborted": true,
"state": "aborted"
}))
.unwrap();
assert!(response.aborted);
assert_eq!(response.state, "aborted");
}
#[test]
fn pull_missing_content_is_append_only_and_operator_adjudicated() {
let legacy: PullResponse = serde_json::from_value(serde_json::json!({
"head": null,
"branch": null,
"branches": [],
"commits": [],
"blobs": [],
"trees": []
}))
.unwrap();
assert!(legacy.missing_content.is_empty());
let response: PullResponse = serde_json::from_value(serde_json::json!({
"head": null,
"branch": null,
"branches": [],
"commits": [],
"blobs": [],
"trees": [],
"missing_content": [{
"kind": "blob",
"hash": "ab".repeat(32),
"reason_code": "operator_adjudicated_loss"
}]
}))
.unwrap();
assert_eq!(response.missing_content.len(), 1);
assert_eq!(response.missing_content[0].kind, "blob");
assert_eq!(
response.missing_content[0].reason_code,
"operator_adjudicated_loss"
);
}
#[test]
fn pull_known_loss_protocol_is_explicit_and_omitted_for_legacy_requests() {
let mut query = PullQuery {
since: None,
branch_name: None,
force: false,
since_rename_id: 0,
depth: None,
known_loss_protocol: None,
retention_protocol: None,
};
let legacy = serde_json::to_value(&query).unwrap();
assert!(legacy.get("known_loss_protocol").is_none());
assert!(legacy.get("retention_protocol").is_none());
query.known_loss_protocol = Some("report_v1".to_string());
query.retention_protocol = Some(RETENTION_PROTOCOL.to_string());
let opted_in = serde_json::to_value(&query).unwrap();
assert_eq!(opted_in["known_loss_protocol"], "report_v1");
assert_eq!(opted_in["retention_protocol"], "expired_v1");
}
#[test]
fn chunk_batch_protocol_is_append_only_and_explicit() {
let legacy_check: ChunkCheckRequest = serde_json::from_value(serde_json::json!({
"hashes": [],
"sizes": []
}))
.unwrap();
assert_eq!(legacy_check.chunk_batch_protocol, None);
let legacy_download: ChunkDownloadRequest =
serde_json::from_value(serde_json::json!({ "hashes": [] })).unwrap();
assert_eq!(legacy_download.chunk_batch_protocol, None);
let check = serde_json::to_value(ChunkCheckRequest {
hashes: vec!["ab".repeat(32)],
sizes: Some(vec![1]),
chunk_batch_protocol: Some("bounded_v1".to_string()),
})
.unwrap();
assert_eq!(check["chunk_batch_protocol"], "bounded_v1");
let download = serde_json::to_value(ChunkDownloadRequest {
hashes: vec!["ab".repeat(32)],
chunk_batch_protocol: Some("bounded_v1".to_string()),
})
.unwrap();
assert_eq!(download["chunk_batch_protocol"], "bounded_v1");
}
fn sample_tree() -> Tree {
Tree::new(vec![
TreeEntry {
name: "a.txt".to_string(),
kind: TreeEntryKind::Blob,
hash: Hash::from_hex(&"11".repeat(32)).unwrap(),
mode: FileMode::Regular,
},
TreeEntry {
name: "b.txt".to_string(),
kind: TreeEntryKind::Blob,
hash: Hash::from_hex(&"22".repeat(32)).unwrap(),
mode: FileMode::Regular,
},
])
.unwrap()
}
#[test]
fn tree_data_to_core_round_trips_a_valid_tree() {
let tree = sample_tree();
let wire = tree_to_wire(&tree);
let back = tree_data_to_core(&wire).expect("valid tree must convert");
assert_eq!(back.hash, tree.hash);
assert_eq!(back.entries.len(), 2);
}
#[test]
fn tree_data_to_core_rejects_tampered_entry_under_claimed_hash() {
let tree = sample_tree();
let mut wire = tree_to_wire(&tree);
wire.entries[0].hash = "99".repeat(32); let err = tree_data_to_core(&wire)
.expect_err("a tree whose content doesn't match its hash must be rejected");
assert!(
err.contains("hash mismatch"),
"expected a hash-mismatch error, got: {err}"
);
}
#[test]
fn tree_data_to_core_canonicalizes_entry_order() {
let tree = sample_tree();
let mut wire = tree_to_wire(&tree);
wire.entries.reverse();
let back = tree_data_to_core(&wire).expect("order must not affect validity");
assert_eq!(back.hash, tree.hash);
assert_eq!(back.entries[0].name, "a.txt");
}
#[test]
fn tree_data_to_core_rejects_invalid_mode() {
let tree = sample_tree();
let mut wire = tree_to_wire(&tree);
wire.entries[0].mode = "mystery".to_string();
let err = tree_data_to_core(&wire).expect_err("invalid mode must be rejected");
assert!(
err.contains("invalid tree entry mode"),
"unexpected error: {err}"
);
}
fn commit_to_wire(commit: &Commit) -> CommitData {
CommitData {
hash: commit.hash.to_string(),
branch_name: commit.branch_name.clone(),
parent_hash: commit.parent_hash.as_ref().map(|h| h.to_string()),
merge_parent_hash: commit.merge_parent_hash.as_ref().map(|h| h.to_string()),
manifest_hash: commit.manifest_hash.to_string(),
author: commit.author.clone(),
message: commit.message.clone(),
timestamp: commit.timestamp.to_rfc3339(),
files: commit
.files
.iter()
.map(|f| FileChangeData {
path: f.path.clone(),
change_type: match f.change_type {
ChangeType::Added => "added".to_string(),
ChangeType::Modified => "modified".to_string(),
ChangeType::Deleted => "deleted".to_string(),
ChangeType::Renamed => "renamed".to_string(),
},
old_blob_hash: f.old_blob_hash.as_ref().map(|h| h.to_string()),
new_blob_hash: f.new_blob_hash.as_ref().map(|h| h.to_string()),
old_path: f.old_path.clone(),
old_mode: f.old_mode.map(|mode| file_mode_to_wire(mode).to_string()),
new_mode: f.new_mode.map(|mode| file_mode_to_wire(mode).to_string()),
})
.collect(),
}
}
fn sample_commit() -> CommitData {
let timestamp = chrono::DateTime::parse_from_rfc3339("2026-06-24T00:00:00Z")
.unwrap()
.with_timezone(&chrono::Utc);
let commit = Commit::with_timestamp(
"main".to_string(),
None,
None,
Tree::empty_hash(),
"tester".to_string(),
None,
vec![FileChange {
path: "src/lib.rs".to_string(),
change_type: ChangeType::Modified,
old_blob_hash: Some(Hash::from_hex(&"11".repeat(32)).unwrap()),
new_blob_hash: Some(Hash::from_hex(&"22".repeat(32)).unwrap()),
old_path: None,
old_mode: None,
new_mode: None,
}],
timestamp,
)
.unwrap();
commit_to_wire(&commit)
}
#[test]
fn commit_data_to_core_round_trips_valid_commit() {
let wire = sample_commit();
let commit = commit_data_to_core(&wire).expect("valid commit must convert");
assert_eq!(commit.hash.to_string(), wire.hash);
assert_eq!(commit.branch_name, "main");
assert_eq!(commit.files.len(), 1);
}
#[test]
fn commit_data_to_core_hash_is_stable() {
let wire = sample_commit();
assert_eq!(
wire.hash,
"93dd864faf6f23324fa2010c7f6d51b5aa456686586a2b65c132575661866b0a"
);
}
#[test]
fn commit_data_to_core_rejects_hash_mismatch() {
let mut wire = sample_commit();
wire.hash = "99".repeat(32);
let err = commit_data_to_core(&wire).expect_err("tampered commit hash must be rejected");
assert!(
err.contains("commit hash mismatch"),
"expected a commit hash mismatch, got: {err}"
);
}
#[test]
fn commit_data_to_core_recovers_micro_truncated_timestamp() {
let nano_ts = chrono::DateTime::parse_from_rfc3339("2026-07-04T08:25:15.778307228Z")
.unwrap()
.with_timezone(&chrono::Utc);
let commit = Commit::with_timestamp(
"main".to_string(),
None,
None,
Tree::empty_hash(),
"tester".to_string(),
Some("squash message".to_string()),
Vec::new(),
nano_ts,
)
.unwrap();
let mut wire = commit_to_wire(&commit);
wire.timestamp = "2026-07-04T08:25:15.778307+00:00".to_string();
let back = commit_data_to_core(&wire).expect("truncated timestamp must be recovered");
assert_eq!(back.hash, commit.hash);
assert_eq!(back.timestamp, nano_ts);
let mut tampered = commit_to_wire(&commit);
tampered.timestamp = "2026-07-04T08:25:15.778307+00:00".to_string();
tampered.author = "someone-else".to_string();
let err = commit_data_to_core(&tampered).expect_err("tampered fields must be rejected");
assert!(
err.contains("commit hash mismatch"),
"expected a commit hash mismatch, got: {err}"
);
}
#[test]
fn commit_data_to_core_rejects_invalid_hash_fields_and_change_types() {
let mut wire = sample_commit();
wire.parent_hash = Some("not-a-hash".to_string());
let err = commit_data_to_core(&wire).expect_err("invalid parent hash must be rejected");
assert!(
err.contains("invalid parent hash"),
"unexpected error: {err}"
);
let mut wire = sample_commit();
wire.files[0].change_type = "rewritten".to_string();
let err = commit_data_to_core(&wire).expect_err("invalid change type must be rejected");
assert!(
err.contains("invalid file change #0 type"),
"unexpected error: {err}"
);
}
}