Skip to main content

cli/git_projection_engine/
git_notes.rs

1// SPDX-License-Identifier: Apache-2.0
2#![deny(clippy::cast_possible_truncation)]
3
4//! Git notes attached at `refs/notes/heddle` carry Heddle state metadata
5//! (change_id, agent, confidence, status) without polluting the commit
6//! message — and so without changing the commit SHA.
7//!
8//! This is the history-carrying half of the export identity model. The
9//! `git-projection-mapping.json` sidecar is a local served/export cache; notes are
10//! the portable source that survives plain Git clones and exports.
11//!
12//! Sley provides the tree-backed notes plumbing; this module owns Heddle's
13//! JSON payload and the fixed `refs/notes/heddle` location.
14
15use 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::{GitProjectionError, GitProjectionResult, git_err};
25
26/// The notes ref heddle uses. Git-compatible notes readers can opt into
27/// this location, while Heddle reads and writes it natively.
28pub const NOTES_REF: &str = "refs/notes/heddle";
29
30/// JSON payload stored inside each note blob.
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub struct HeddleNote {
33    /// The heddle change_id this commit corresponds to.
34    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    /// Either "draft" or "published".
40    pub status: String,
41    // --- W2/R6 tail fields below; new fields go here. All optional + skip-if-none. ---
42    /// Per-scope counts of annotations dropped at export because their
43    /// visibility exceeded the export's audience tier. Populated when the
44    /// caller exports with `--notes` and `--audience`.
45    #[serde(default, skip_serializing_if = "Option::is_none")]
46    pub omitted_annotations_breakdown: Option<OmittedBreakdown>,
47    /// Per-module signal counts on the state at export time. Read-only
48    /// metadata for downstream tooling.
49    #[serde(default, skip_serializing_if = "Option::is_none")]
50    pub signal_counts: Option<SignalCounts>,
51    /// Author + agent attribution rolled up into a richer shape than the
52    /// commit's own author signature can carry.
53    #[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/// Per-scope omitted-annotation counts emitted alongside `refs/notes/heddle`.
64#[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/// Per-module risk-signal fire counts on this state.
75#[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    /// Construct a note from a heddle state (the form written on export).
99    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    /// R6 builder: set the per-scope omitted-annotation breakdown.
120    pub fn with_omitted_breakdown(mut self, breakdown: OmittedBreakdown) -> Self {
121        self.omitted_annotations_breakdown = Some(breakdown);
122        self
123    }
124
125    /// R6 builder: set the per-module signal counts.
126    pub fn with_signal_counts(mut self, counts: SignalCounts) -> Self {
127        self.signal_counts = Some(counts);
128        self
129    }
130
131    /// R6 builder: set richer attribution (principal + agent).
132    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) -> GitProjectionResult<Vec<u8>> {
138        serde_json::to_vec_pretty(self)
139            .map_err(|e| GitProjectionError::Git(format!("note serialize: {e}")))
140    }
141
142    pub fn from_json_bytes(bytes: &[u8]) -> GitProjectionResult<Self> {
143        serde_json::from_slice(bytes)
144            .map_err(|e| GitProjectionError::Git(format!("note parse: {e}")))
145    }
146}
147
148fn notes_ref() -> sley::notes::NotesRef {
149    sley::notes::NotesRef::expand(NOTES_REF)
150}
151
152/// Attach `note` to `commit_oid` in `repo` under `refs/notes/heddle`.
153///
154/// Each call creates one new notes commit on top of any previous notes
155/// history. The notes ref is updated atomically via sley's notes plumbing.
156pub fn write_note(
157    repo: &Repository,
158    commit_oid: ObjectId,
159    note: &HeddleNote,
160) -> GitProjectionResult<()> {
161    let json = note.to_json_bytes()?;
162    let notes_ref = notes_ref();
163    let refs = repo.references();
164    sley::notes::upsert_note_bytes_for(
165        repo.git_dir(),
166        repo.object_format(),
167        &refs,
168        &notes_ref,
169        &commit_oid,
170        &json,
171        "heddle: state metadata",
172        &git_projection_notes_identity(),
173        sley::notes::notes_ref_expected(&refs, &notes_ref).map_err(git_err)?,
174    )
175    .map_err(git_err)?;
176    Ok(())
177}
178
179/// Retract the notes attached to `commit_oids` from `refs/notes/heddle`.
180///
181/// The notes ref copies to the public mirror alongside branches and tags
182/// (`collect_ref_updates` picks up `refs/notes/*`), so a note left behind for a
183/// commit that has since been embargoed/retracted is a metadata leak: the
184/// mirror keeps publishing a note whose payload (and tree entry) references the
185/// withheld commit. This is the notes-ref sibling of the branch/tag retraction
186/// the exporter already performs (heddle#316).
187///
188/// Writes a single new notes commit dropping every present entry, then advances
189/// `refs/notes/heddle` to it. A genuine fast-forward (the new commit descends
190/// from the prior notes head), so it survives the bridge's FF guard on push.
191/// No-op — no new commit, no ref churn — when the notes ref is absent or none
192/// of `commit_oids` actually has an entry.
193pub fn remove_notes(
194    repo: &Repository,
195    commit_oids: &std::collections::HashSet<ObjectId>,
196) -> GitProjectionResult<()> {
197    if commit_oids.is_empty() {
198        return Ok(());
199    }
200    let notes_ref = notes_ref();
201    let refs = repo.references();
202    let annotated: Vec<ObjectId> = commit_oids.iter().copied().collect();
203    sley::notes::remove_notes_for(
204        repo.git_dir(),
205        repo.object_format(),
206        &refs,
207        &notes_ref,
208        &annotated,
209        "heddle: retract state metadata",
210        &git_projection_notes_identity(),
211        sley::notes::notes_ref_expected(&refs, &notes_ref).map_err(git_err)?,
212    )
213    .map_err(git_err)?;
214    Ok(())
215}
216
217/// Look up the note attached to `commit_oid`, if any.
218pub fn read_note(
219    repo: &Repository,
220    commit_oid: ObjectId,
221) -> GitProjectionResult<Option<HeddleNote>> {
222    let Some(bytes) = repo
223        .read_note_bytes(&notes_ref(), &commit_oid)
224        .map_err(git_err)?
225    else {
226        return Ok(None);
227    };
228    HeddleNote::from_json_bytes(&bytes).map(Some)
229}
230
231/// Read every portable Git↔Heddle identity recorded under `refs/notes/heddle`.
232pub(crate) fn read_identity_mappings(
233    repo: &Repository,
234) -> GitProjectionResult<Vec<(ChangeId, ObjectId)>> {
235    read_all_notes(repo)?
236        .into_iter()
237        .map(|(oid, note)| Ok((ChangeId::parse(&note.change_id)?, oid)))
238        .collect()
239}
240
241/// Read every (commit_oid → note) entry under `refs/notes/heddle`.
242pub(crate) fn read_all_notes(
243    repo: &Repository,
244) -> GitProjectionResult<HashMap<ObjectId, HeddleNote>> {
245    let mut out = HashMap::new();
246    for note_entry in repo.list_notes(&notes_ref()).map_err(git_err)? {
247        let object = repo.read_object(&note_entry.blob).map_err(git_err)?;
248        // Skip entries that aren't well-formed heddle notes — could be left
249        // over from `git notes --ref=heddle add` by an external tool.
250        if object.object_type != sley::GitObjectType::Blob {
251            continue;
252        }
253        if let Ok(note) = HeddleNote::from_json_bytes(&object.body) {
254            out.insert(note_entry.annotated, note);
255        }
256    }
257    Ok(out)
258}
259
260fn git_projection_notes_identity() -> sley::notes::NotesCommitIdentity {
261    let seconds = SystemTime::now()
262        .duration_since(UNIX_EPOCH)
263        .map(|d| d.as_secs() as i64)
264        .unwrap_or(0);
265    let ident = format!("Heddle <heddle@local> {seconds} +0000").into_bytes();
266    sley::notes::NotesCommitIdentity {
267        author: ident.clone(),
268        committer: ident,
269    }
270}