1#![deny(clippy::cast_possible_truncation)]
3
4use std::{
16 collections::HashMap,
17 time::{SystemTime, UNIX_EPOCH},
18};
19
20use objects::object::{ChangeId, State, Status};
21use serde::{Deserialize, Serialize};
22use sley::{ObjectId, Repository};
23
24use super::git_core::{GitBridgeError, GitResult, git_err};
25
26pub const NOTES_REF: &str = "refs/notes/heddle";
29
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub struct HeddleNote {
33 pub change_id: String,
35 #[serde(default, skip_serializing_if = "Option::is_none")]
36 pub agent: Option<NoteAgent>,
37 #[serde(default, skip_serializing_if = "Option::is_none")]
38 pub confidence: Option<f32>,
39 pub status: String,
41 #[serde(default, skip_serializing_if = "Option::is_none")]
46 pub omitted_annotations_breakdown: Option<OmittedBreakdown>,
47 #[serde(default, skip_serializing_if = "Option::is_none")]
50 pub signal_counts: Option<SignalCounts>,
51 #[serde(default, skip_serializing_if = "Option::is_none")]
54 pub attribution: Option<NoteAttribution>,
55}
56
57#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
58pub struct NoteAgent {
59 pub provider: String,
60 pub model: String,
61}
62
63#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
65pub struct OmittedBreakdown {
66 #[serde(default)]
67 pub internal: u32,
68 #[serde(default)]
69 pub team: u32,
70 #[serde(default)]
71 pub restricted: u32,
72}
73
74#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
76pub struct SignalCounts {
77 #[serde(default)]
78 pub novelty: u32,
79 #[serde(default)]
80 pub test_reachability: u32,
81 #[serde(default)]
82 pub pattern_deviation: u32,
83 #[serde(default)]
84 pub invariant_adjacency: u32,
85 #[serde(default)]
86 pub self_flagged_uncertainty: u32,
87}
88
89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
90pub struct NoteAttribution {
91 pub principal_name: String,
92 pub principal_email: String,
93 #[serde(default, skip_serializing_if = "Option::is_none")]
94 pub agent: Option<NoteAgent>,
95}
96
97impl HeddleNote {
98 pub fn from_state(state: &State) -> Self {
100 let status = match state.status {
101 Status::Draft => "draft".to_string(),
102 Status::Published => "published".to_string(),
103 };
104 let agent = state.attribution.agent.as_ref().map(|a| NoteAgent {
105 provider: a.provider.clone(),
106 model: a.model.clone(),
107 });
108 Self {
109 change_id: state.change_id.to_string_full(),
110 agent,
111 confidence: state.confidence,
112 status,
113 omitted_annotations_breakdown: None,
114 signal_counts: None,
115 attribution: None,
116 }
117 }
118
119 pub fn with_omitted_breakdown(mut self, breakdown: OmittedBreakdown) -> Self {
121 self.omitted_annotations_breakdown = Some(breakdown);
122 self
123 }
124
125 pub fn with_signal_counts(mut self, counts: SignalCounts) -> Self {
127 self.signal_counts = Some(counts);
128 self
129 }
130
131 pub fn with_attribution(mut self, attribution: NoteAttribution) -> Self {
133 self.attribution = Some(attribution);
134 self
135 }
136
137 pub fn to_json_bytes(&self) -> GitResult<Vec<u8>> {
138 serde_json::to_vec_pretty(self)
139 .map_err(|e| GitBridgeError::Git(format!("note serialize: {e}")))
140 }
141
142 pub fn from_json_bytes(bytes: &[u8]) -> GitResult<Self> {
143 serde_json::from_slice(bytes).map_err(|e| GitBridgeError::Git(format!("note parse: {e}")))
144 }
145}
146
147fn notes_ref() -> sley::notes::NotesRef {
148 sley::notes::NotesRef::expand(NOTES_REF)
149}
150
151pub fn write_note(repo: &Repository, commit_oid: ObjectId, note: &HeddleNote) -> GitResult<()> {
156 let json = note.to_json_bytes()?;
157 let notes_ref = notes_ref();
158 let refs = repo.references();
159 sley::notes::upsert_note_bytes_for(
160 repo.git_dir(),
161 repo.object_format(),
162 &refs,
163 ¬es_ref,
164 &commit_oid,
165 &json,
166 "heddle: state metadata",
167 &bridge_notes_identity(),
168 sley::notes::notes_ref_expected(&refs, ¬es_ref).map_err(git_err)?,
169 )
170 .map_err(git_err)?;
171 Ok(())
172}
173
174pub fn remove_notes(
189 repo: &Repository,
190 commit_oids: &std::collections::HashSet<ObjectId>,
191) -> GitResult<()> {
192 if commit_oids.is_empty() {
193 return Ok(());
194 }
195 let notes_ref = notes_ref();
196 let refs = repo.references();
197 let annotated: Vec<ObjectId> = commit_oids.iter().copied().collect();
198 sley::notes::remove_notes_for(
199 repo.git_dir(),
200 repo.object_format(),
201 &refs,
202 ¬es_ref,
203 &annotated,
204 "heddle: retract state metadata",
205 &bridge_notes_identity(),
206 sley::notes::notes_ref_expected(&refs, ¬es_ref).map_err(git_err)?,
207 )
208 .map_err(git_err)?;
209 Ok(())
210}
211
212pub fn read_note(repo: &Repository, commit_oid: ObjectId) -> GitResult<Option<HeddleNote>> {
214 let Some(bytes) = repo
215 .read_note_bytes(¬es_ref(), &commit_oid)
216 .map_err(git_err)?
217 else {
218 return Ok(None);
219 };
220 HeddleNote::from_json_bytes(&bytes).map(Some)
221}
222
223pub(crate) fn read_identity_mappings(repo: &Repository) -> GitResult<Vec<(ChangeId, ObjectId)>> {
225 read_all_notes(repo)?
226 .into_iter()
227 .map(|(oid, note)| Ok((ChangeId::parse(¬e.change_id)?, oid)))
228 .collect()
229}
230
231pub(crate) fn read_all_notes(repo: &Repository) -> GitResult<HashMap<ObjectId, HeddleNote>> {
233 let mut out = HashMap::new();
234 for note_entry in repo.list_notes(¬es_ref()).map_err(git_err)? {
235 let object = repo.read_object(¬e_entry.blob).map_err(git_err)?;
236 if object.object_type != sley::GitObjectType::Blob {
239 continue;
240 }
241 if let Ok(note) = HeddleNote::from_json_bytes(&object.body) {
242 out.insert(note_entry.annotated, note);
243 }
244 }
245 Ok(out)
246}
247
248fn bridge_notes_identity() -> sley::notes::NotesCommitIdentity {
249 let seconds = SystemTime::now()
250 .duration_since(UNIX_EPOCH)
251 .map(|d| d.as_secs() as i64)
252 .unwrap_or(0);
253 let ident = format!("Heddle <heddle@local> {seconds} +0000").into_bytes();
254 sley::notes::NotesCommitIdentity {
255 author: ident.clone(),
256 committer: ident,
257 }
258}