1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! NAP Commit — the history primitive.
//!
//! A commit records a point-in-time snapshot of a manifest, plus
//! patch metadata describing what changed. This is Option C from the
//! design requirements: **Snapshot + Patch Metadata**.
//!
//! - The VCS (Git) stores the full snapshot (tree).
//! - The commit object stores change descriptions (patches) for efficient
//! audit/provenance without requiring full diff reconstruction.
//!
//! Commits are NOT stored inside the manifest. The manifest stores only
//! `head` — a pointer to the latest commit hash.
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
/// A NAP commit — snapshot + patch metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Commit {
/// BLAKE3 of commit content (self-referential hash).
pub id: String,
/// Parent commit hash. `None` for the initial commit.
pub parent: Option<String>,
/// When this commit was created.
pub timestamp: DateTime<Utc>,
/// Author identifier (DID key, email, or key fingerprint).
pub author: String,
/// Ed25519 signature over the commit hash (optional for v0).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub signature: Option<String>,
/// Human-readable commit message.
pub message: String,
/// BLAKE3 content hash of the resulting manifest after this commit.
pub manifest_hash: String,
/// What changed in this commit (patch metadata for audit).
#[serde(default)]
pub changes: Vec<Change>,
}
/// A single change within a commit.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Change {
/// Dot-notation path to the changed field.
/// e.g., `"properties.homeworld"`, `"representations.reference_image.hash"`.
pub path: String,
/// The kind of change.
pub operation: ChangeOp,
/// Previous value hash (for verification).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub old_value: Option<String>,
/// New value hash or literal (for small values).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub new_value: Option<String>,
}
/// The type of change operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ChangeOp {
/// Set or update a field.
Set,
/// Delete a field.
Delete,
/// Append to an array/list.
Append,
/// Remove from an array/list.
Remove,
}
impl Commit {
/// Create a new commit. The `id` is computed after construction
/// by hashing the serialized content.
pub fn new(
parent: Option<String>,
author: &str,
message: &str,
manifest_hash: &str,
changes: Vec<Change>,
) -> Self {
let mut commit = Self {
id: String::new(), // Placeholder — computed below
parent,
timestamp: Utc::now(),
author: author.to_string(),
signature: None,
message: message.to_string(),
manifest_hash: manifest_hash.to_string(),
changes,
};
commit.id = commit.compute_id();
commit
}
/// Compute the content-addressed ID (BLAKE3) of this commit.
/// Hashes: parent + timestamp + author + message + manifest_hash + changes.
fn compute_id(&self) -> String {
let mut hasher = blake3::Hasher::new();
hasher.update(self.parent.as_deref().unwrap_or("root").as_bytes());
hasher.update(self.timestamp.to_rfc3339().as_bytes());
hasher.update(self.author.as_bytes());
hasher.update(self.message.as_bytes());
hasher.update(self.manifest_hash.as_bytes());
// Include change paths and ops for determinism
for change in &self.changes {
hasher.update(change.path.as_bytes());
hasher.update(format!("{:?}", change.operation).as_bytes());
}
let digest = hasher.finalize();
hex::encode(digest.as_bytes())
}
/// Re-compute and verify the commit ID matches the stored value.
pub fn verify_id(&self) -> bool {
self.id == self.compute_id()
}
}
impl Change {
/// Create a `Set` change.
pub fn set(path: &str, old_value: Option<String>, new_value: String) -> Self {
Self {
path: path.to_string(),
operation: ChangeOp::Set,
old_value,
new_value: Some(new_value),
}
}
/// Create a `Delete` change.
pub fn delete(path: &str, old_value: String) -> Self {
Self {
path: path.to_string(),
operation: ChangeOp::Delete,
old_value: Some(old_value),
new_value: None,
}
}
/// Create an `Append` change.
pub fn append(path: &str, new_value: String) -> Self {
Self {
path: path.to_string(),
operation: ChangeOp::Append,
old_value: None,
new_value: Some(new_value),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_commit_id_determinism() {
// Two commits with same content should get the same ID
// (except timestamp — so we can't test exact equality easily).
// Instead, verify that id matches recompute.
let commit = Commit::new(
None,
"test-author",
"initial commit",
"blake3:0000000000000000000000000000000000000000000000000000000000000000",
vec![Change::set("properties.name", None, "Luke".to_string())],
);
assert!(commit.verify_id());
}
#[test]
fn test_commit_with_parent() {
let parent_commit = Commit::new(
None,
"test-author",
"initial commit",
"blake3:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
vec![],
);
let child_commit = Commit::new(
Some(parent_commit.id.clone()),
"test-author",
"update homeworld",
"blake3:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
vec![Change::set(
"properties.homeworld",
None,
"nap://starwars/location/tatooine".to_string(),
)],
);
assert_eq!(child_commit.parent, Some(parent_commit.id));
assert!(child_commit.verify_id());
}
}