Skip to main content

cli/bridge/
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//! `bridge-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::{GitBridgeError, GitResult, 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) -> 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
151/// Attach `note` to `commit_oid` in `repo` under `refs/notes/heddle`.
152///
153/// Each call creates one new notes commit on top of any previous notes
154/// history. The notes ref is updated atomically via sley's notes plumbing.
155pub 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        &notes_ref,
164        &commit_oid,
165        &json,
166        "heddle: state metadata",
167        &bridge_notes_identity(),
168        sley::notes::notes_ref_expected(&refs, &notes_ref).map_err(git_err)?,
169    )
170    .map_err(git_err)?;
171    Ok(())
172}
173
174/// Retract the notes attached to `commit_oids` from `refs/notes/heddle`.
175///
176/// The notes ref copies to the public mirror alongside branches and tags
177/// (`collect_ref_updates` picks up `refs/notes/*`), so a note left behind for a
178/// commit that has since been embargoed/retracted is a metadata leak: the
179/// mirror keeps publishing a note whose payload (and tree entry) references the
180/// withheld commit. This is the notes-ref sibling of the branch/tag retraction
181/// the exporter already performs (heddle#316).
182///
183/// Writes a single new notes commit dropping every present entry, then advances
184/// `refs/notes/heddle` to it. A genuine fast-forward (the new commit descends
185/// from the prior notes head), so it survives the bridge's FF guard on push.
186/// No-op — no new commit, no ref churn — when the notes ref is absent or none
187/// of `commit_oids` actually has an entry.
188pub 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        &notes_ref,
203        &annotated,
204        "heddle: retract state metadata",
205        &bridge_notes_identity(),
206        sley::notes::notes_ref_expected(&refs, &notes_ref).map_err(git_err)?,
207    )
208    .map_err(git_err)?;
209    Ok(())
210}
211
212/// Look up the note attached to `commit_oid`, if any.
213pub fn read_note(repo: &Repository, commit_oid: ObjectId) -> GitResult<Option<HeddleNote>> {
214    let Some(bytes) = repo
215        .read_note_bytes(&notes_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
223/// Read every portable Git↔Heddle identity recorded under `refs/notes/heddle`.
224pub(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(&note.change_id)?, oid)))
228        .collect()
229}
230
231/// Read every (commit_oid → note) entry under `refs/notes/heddle`.
232pub(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(&notes_ref()).map_err(git_err)? {
235        let object = repo.read_object(&note_entry.blob).map_err(git_err)?;
236        // Skip entries that aren't well-formed heddle notes — could be left
237        // over from `git notes --ref=heddle add` by an external tool.
238        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}