agentmux_srv_core/backend/agent_archive/
manifest.rs1use serde::{Deserialize, Serialize};
13
14pub const KIND: &str = "agent";
16pub const FORMAT: u32 = 1;
18pub const CHUNK_BYTES: u64 = 256 * 1024;
20pub const MANIFEST_NAME: &str = "manifest.json";
22pub const SIGN_DOMAIN: &[u8] = b"amx-agent-archive-v1\0";
25
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
28#[serde(deny_unknown_fields)]
29pub struct Part {
30 pub path: String,
31 pub size: u64,
32 pub sha256: String,
34 pub chunks: Vec<String>,
36}
37
38#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
40#[serde(deny_unknown_fields)]
41pub struct AgentInfo {
42 pub uid: String,
43 pub name: String,
44 pub slug: String,
45}
46
47#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
49#[serde(deny_unknown_fields)]
50pub struct Source {
51 pub instance_id: String,
52 pub host_hint: String,
53 pub app_version: String,
55 pub os: String,
57}
58
59#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
61#[serde(deny_unknown_fields)]
62pub struct Signature {
63 pub alg: String,
64 pub public_key: String,
67 pub value: String,
69}
70
71#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
72#[serde(deny_unknown_fields)]
73pub struct Manifest {
74 pub kind: String,
75 pub format: u32,
76 pub created_at: i64,
78 pub agent: AgentInfo,
79 pub source: Source,
80 pub parts: Vec<Part>,
81 #[serde(default, skip_serializing_if = "Option::is_none")]
82 pub signature: Option<Signature>,
83}
84
85impl Manifest {
86 pub fn signed_bytes(&self) -> Vec<u8> {
88 let mut bare = self.clone();
89 bare.signature = None;
90 let mut out = SIGN_DOMAIN.to_vec();
91 out.extend(serde_json::to_vec(&bare).unwrap_or_default());
92 out
93 }
94
95 pub fn part(&self, path: &str) -> Option<&Part> {
97 self.parts.iter().find(|p| p.path == path)
98 }
99
100 pub fn total_bytes(&self) -> u64 {
102 self.parts.iter().map(|p| p.size).sum()
103 }
104}
105
106#[derive(Debug, Clone, PartialEq, Eq)]
108pub enum SignatureState {
109 Unsigned,
111 Valid { public_key: [u8; 32] },
113 Invalid,
115}
116
117pub fn chunk_count(size: u64) -> u64 {
119 size.div_ceil(CHUNK_BYTES)
120}
121
122pub fn valid_part_path(path: &str) -> bool {
125 if path.is_empty() || path.len() > 240 || path == MANIFEST_NAME {
126 return false;
127 }
128 if path.chars().any(|c| c.is_control() || matches!(c, '\\' | ':' | '<' | '>' | '"' | '|' | '?' | '*')) {
129 return false;
130 }
131 path.split('/').all(|part| {
132 if part.is_empty() || part == "." || part == ".." || part.ends_with('.') || part.ends_with(' ') || part.starts_with(' ') {
133 return false;
134 }
135 let stem = part.split('.').next().unwrap_or("").to_ascii_uppercase();
136 !matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL" | "CLOCK$")
137 && !(stem.len() == 4 && (stem.starts_with("COM") || stem.starts_with("LPT")) && stem.as_bytes()[3].is_ascii_digit())
138 })
139}
140
141#[cfg(test)]
142mod tests {
143 use super::*;
144
145 #[test]
146 fn part_paths_are_plain_relative_names() {
147 for ok in ["agent.json", "memory/notes.md", "history/archive-1700.jsonl", "a/b/c.txt"] {
148 assert!(valid_part_path(ok), "{ok}");
149 }
150 for bad in [
151 "", "/abs", "../x", "a/../b", "a//b", "./a", "a\\b", "C:/x", "c:x", "a/", "con", "memory/NUL.txt", "lpt1", "a.", "a /b", "manifest.json", "x\u{0}y",
152 "a?b",
153 ] {
154 assert!(!valid_part_path(bad), "{bad:?}");
155 }
156 }
157
158 #[test]
159 fn a_part_of_one_byte_over_a_chunk_has_two_chunks() {
160 assert_eq!(chunk_count(0), 0);
161 assert_eq!(chunk_count(1), 1);
162 assert_eq!(chunk_count(CHUNK_BYTES), 1);
163 assert_eq!(chunk_count(CHUNK_BYTES + 1), 2);
164 }
165}