use crate::error::{GraphError, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct CommitId([u8; 32]);
impl CommitId {
pub const fn from_bytes(bytes: [u8; 32]) -> Self {
Self(bytes)
}
pub const fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
pub fn to_hex(&self) -> String {
hex::encode(self.0)
}
}
impl fmt::Display for CommitId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_hex())
}
}
impl fmt::LowerHex for CommitId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in &self.0 {
write!(f, "{byte:02x}")?;
}
Ok(())
}
}
impl fmt::UpperHex for CommitId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in &self.0 {
write!(f, "{byte:02X}")?;
}
Ok(())
}
}
impl fmt::Octal for CommitId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, byte) in self.0.iter().enumerate() {
if i > 0 {
f.write_str(".")?;
}
write!(f, "{byte:03o}")?;
}
Ok(())
}
}
impl fmt::Binary for CommitId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, byte) in self.0.iter().enumerate() {
if i > 0 {
f.write_str(".")?;
}
write!(f, "{byte:08b}")?;
}
Ok(())
}
}
impl FromStr for CommitId {
type Err = GraphError;
fn from_str(s: &str) -> Result<Self> {
let bytes = hex::decode(s).map_err(|e| GraphError::InvalidCommitId(format!("{s}: {e}")))?;
if bytes.len() != 32 {
return Err(GraphError::InvalidCommitId(format!(
"expected 32 bytes, got {}",
bytes.len()
)));
}
let mut arr = [0u8; 32];
arr.copy_from_slice(&bytes);
Ok(CommitId(arr))
}
}
impl TryFrom<String> for CommitId {
type Error = GraphError;
fn try_from(value: String) -> Result<Self> {
CommitId::from_str(&value)
}
}
impl From<CommitId> for String {
fn from(value: CommitId) -> Self {
value.to_hex()
}
}
mod hex {
pub fn encode(bytes: [u8; 32]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
#[derive(Debug)]
pub struct HexError(pub String);
impl std::fmt::Display for HexError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "invalid hex: {}", self.0)
}
}
pub fn decode(s: &str) -> std::result::Result<Vec<u8>, HexError> {
if !s.len().is_multiple_of(2) {
return Err(HexError(s.to_string()));
}
let mut out = Vec::with_capacity(s.len() / 2);
let bytes = s.as_bytes();
for chunk in bytes.chunks(2) {
let hi = (chunk[0] as char)
.to_digit(16)
.ok_or_else(|| HexError(s.to_string()))?;
let lo = (chunk[1] as char)
.to_digit(16)
.ok_or_else(|| HexError(s.to_string()))?;
out.push(((hi << 4) | lo) as u8);
}
Ok(out)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Author {
#[default]
User,
Assistant,
Tool,
System,
}
impl fmt::Display for Author {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Author::User => "user",
Author::Assistant => "assistant",
Author::Tool => "tool",
Author::System => "system",
};
write!(f, "{s}")
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Delta {
Message {
content: String,
},
ToolCall {
call_id: String,
name: String,
arguments: serde_json::Value,
},
ToolResult {
call_id: String,
result: serde_json::Value,
},
Compaction {
replaces: Vec<CommitId>,
summary: String,
},
Merge {
strategy: MergeStrategy,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MergeStrategy {
#[default]
RecordOnly,
PreferOther,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TokenUsage {
pub prompt_tokens: u64,
pub completion_tokens: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Metadata {
pub model_id: Option<String>,
pub token_usage: Option<TokenUsage>,
pub timestamp: DateTime<Utc>,
pub tags: BTreeMap<String, String>,
}
impl Metadata {
pub fn new(timestamp: DateTime<Utc>) -> Self {
Self {
model_id: None,
token_usage: None,
timestamp,
tags: BTreeMap::new(),
}
}
pub fn with_tag(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.tags.insert(key.into(), value.into());
self
}
}
#[derive(Serialize)]
struct CommitContent<'a> {
parent_ids: &'a [CommitId],
author: &'a Author,
delta: &'a Delta,
metadata: &'a Metadata,
}
pub fn compute_commit_id(
parent_ids: &[CommitId],
author: &Author,
delta: &Delta,
metadata: &Metadata,
) -> CommitId {
let content = CommitContent {
parent_ids,
author,
delta,
metadata,
};
let bytes = serde_json::to_vec(&content).expect("commit content is always serializable");
CommitId(*blake3::hash(&bytes).as_bytes())
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Commit {
pub id: CommitId,
pub parent_ids: Vec<CommitId>,
pub author: Author,
pub delta: Delta,
pub metadata: Metadata,
}
impl Commit {
pub fn new(
parent_ids: Vec<CommitId>,
author: Author,
delta: Delta,
metadata: Metadata,
) -> Self {
let id = compute_commit_id(&parent_ids, &author, &delta, &metadata);
Self {
id,
parent_ids,
author,
delta,
metadata,
}
}
pub fn is_root(&self) -> bool {
self.parent_ids.is_empty()
}
pub fn is_merge(&self) -> bool {
self.parent_ids.len() > 1
}
}
#[cfg(test)]
mod tests {
use super::*;
fn ts() -> DateTime<Utc> {
DateTime::parse_from_rfc3339("2026-01-01T00:00:00Z")
.unwrap()
.with_timezone(&Utc)
}
#[test]
fn identical_content_yields_identical_commit_id() {
let delta = Delta::Message {
content: "hello".into(),
};
let meta = Metadata::new(ts()).with_tag("step", "planning");
let id1 = compute_commit_id(&[], &Author::User, &delta, &meta);
let id2 = compute_commit_id(&[], &Author::User, &delta, &meta);
assert_eq!(id1, id2);
}
#[test]
fn different_delta_content_yields_different_commit_id() {
let meta = Metadata::new(ts());
let id1 = compute_commit_id(
&[],
&Author::User,
&Delta::Message {
content: "a".into(),
},
&meta,
);
let id2 = compute_commit_id(
&[],
&Author::User,
&Delta::Message {
content: "b".into(),
},
&meta,
);
assert_ne!(id1, id2);
}
#[test]
fn tag_insertion_order_does_not_affect_commit_id() {
let delta = Delta::Message {
content: "hi".into(),
};
let mut meta_a = Metadata::new(ts());
meta_a.tags.insert("a".into(), "1".into());
meta_a.tags.insert("b".into(), "2".into());
let mut meta_b = Metadata::new(ts());
meta_b.tags.insert("b".into(), "2".into());
meta_b.tags.insert("a".into(), "1".into());
let id_a = compute_commit_id(&[], &Author::User, &delta, &meta_a);
let id_b = compute_commit_id(&[], &Author::User, &delta, &meta_b);
assert_eq!(id_a, id_b);
}
#[test]
fn different_parent_order_yields_different_commit_id() {
let delta = Delta::Message {
content: "merge".into(),
};
let meta = Metadata::new(ts());
let p1 = CommitId::from_bytes([1u8; 32]);
let p2 = CommitId::from_bytes([2u8; 32]);
let id_ab = compute_commit_id(&[p1, p2], &Author::System, &delta, &meta);
let id_ba = compute_commit_id(&[p2, p1], &Author::System, &delta, &meta);
assert_ne!(
id_ab, id_ba,
"parent order is semantically significant for merges"
);
}
#[test]
fn commit_id_round_trips_through_hex_string() {
let id = CommitId::from_bytes([42u8; 32]);
let hex = id.to_hex();
let parsed: CommitId = hex.parse().unwrap();
assert_eq!(id, parsed);
}
#[test]
fn commit_id_from_str_rejects_wrong_length() {
let err = CommitId::from_str("abcd").unwrap_err();
assert!(matches!(err, GraphError::InvalidCommitId(_)));
}
#[test]
fn commit_id_from_str_rejects_non_hex_characters() {
let bad = "zz".repeat(32);
let err = CommitId::from_str(&bad).unwrap_err();
assert!(matches!(err, GraphError::InvalidCommitId(_)));
}
#[test]
fn root_commit_has_no_parents_and_is_recognized_as_root() {
let commit = Commit::new(
vec![],
Author::System,
Delta::Message {
content: "init".into(),
},
Metadata::new(ts()),
);
assert!(commit.is_root());
assert!(!commit.is_merge());
}
#[test]
fn commit_with_two_parents_is_recognized_as_merge() {
let commit = Commit::new(
vec![CommitId::from_bytes([1; 32]), CommitId::from_bytes([2; 32])],
Author::System,
Delta::Message {
content: "merge".into(),
},
Metadata::new(ts()),
);
assert!(commit.is_merge());
assert!(!commit.is_root());
}
#[test]
fn committing_identical_content_twice_is_idempotent_at_the_id_level() {
let meta = Metadata::new(ts());
let c1 = Commit::new(
vec![],
Author::User,
Delta::Message {
content: "x".into(),
},
meta.clone(),
);
let c2 = Commit::new(
vec![],
Author::User,
Delta::Message {
content: "x".into(),
},
meta,
);
assert_eq!(c1.id, c2.id);
}
#[test]
fn empty_message_delta_is_representable_and_hashes_consistently() {
let meta = Metadata::new(ts());
let delta = Delta::Message {
content: String::new(),
};
let id1 = compute_commit_id(&[], &Author::User, &delta, &meta);
let id2 = compute_commit_id(&[], &Author::User, &delta, &meta);
assert_eq!(id1, id2);
}
}