suno_core/hash.rs
1//! Stable content sentinels for change detection.
2//!
3//! Reconcile compares a clip's current [`meta_hash`]/[`art_hash`] against the
4//! manifest to decide whether a file needs re-tagging. The hashes must be stable
5//! across runs, versions, and platforms, so they use FNV-1a over a fixed field
6//! encoding rather than the standard library's deliberately unspecified hasher.
7//!
8//! The hash inputs are the exact fields the tag writer embeds: [`meta_hash`]
9//! hashes the resolved [`TrackMetadata`], and [`art_hash`] tracks the chosen art
10//! URL. Anything embedded in the file is therefore in a hash, so an upstream
11//! change to it triggers a retag; anything not embedded (path-affecting or
12//! sidecar-only fields such as the animated-cover URL) is excluded.
13
14use std::hash::{Hash, Hasher};
15
16use crate::lineage::LineageContext;
17use crate::model::Clip;
18use crate::tag::TrackMetadata;
19use crate::vocab::WebpEncodeSettings;
20
21/// A short, stable hex digest of `bytes` (FNV-1a, 64-bit).
22fn digest(bytes: &[u8]) -> String {
23 let mut hasher = fnv::FnvHasher::default();
24 hasher.write(bytes);
25 format!("{:016x}", hasher.finish())
26}
27
28/// A stable sentinel over an arbitrary generated text artefact.
29///
30/// Used for playlists, whose `.m3u8` body is generated rather than fetched: the
31/// hash is taken over the full rendered text, so the playlist name, the member
32/// order, and every member's relative path, title, and duration all feed it, and
33/// any change that reaches the file triggers a rewrite. The render is
34/// deterministic, so the hash is stable across runs and platforms.
35pub fn content_hash(text: &str) -> String {
36 digest(text.as_bytes())
37}
38
39/// A sentinel for the clip's embedded tag set.
40///
41/// Hashes the resolved [`TrackMetadata`] actually written into the file (title,
42/// artist, album, date/year, lyrics, prompt, model, handle, and the resolved
43/// lineage tags), so a change to any embedded tag triggers a retag while a
44/// change to a non-embedded field (e.g. the animated-cover URL) does not. Taking
45/// [`TrackMetadata`] directly keeps it in lock-step with the tag writer: if a
46/// value is embedded, it is in this hash. Chosen art is tracked separately by
47/// [`art_hash`].
48///
49/// A pure path change is handled as a rename, by comparing the rendered path
50/// with the stored one, not by this hash.
51pub fn meta_hash(clip: &Clip, lineage: &LineageContext) -> String {
52 let mut hasher = fnv::FnvHasher::default();
53 TrackMetadata::from_clip(clip, lineage).hash(&mut hasher);
54 format!("{:016x}", hasher.finish())
55}
56
57/// A stable digest of an artifact source URL (FNV-1a), or the empty string when
58/// `url` is empty.
59///
60/// Shared by [`art_hash`] (the embedded static cover) and the external animated
61/// cover sidecar, whose rewrite detection keys on the clip's `video_cover_url`
62/// rather than the selected image. Keeping both on the one helper means an empty
63/// URL always maps to the empty sentinel, the value reconcile reads as "no such
64/// artifact this run".
65pub fn art_url_hash(url: &str) -> String {
66 if url.is_empty() {
67 String::new()
68 } else {
69 digest(url.as_bytes())
70 }
71}
72
73/// A digest of a transcoded animated-cover source URL *and* the encode settings
74/// that shape its bytes, or the empty string when `url` is empty.
75///
76/// Unlike the raw `cover.mp4` (a verbatim copy, keyed on its URL alone via
77/// [`art_url_hash`]), the animated `cover.webp` and per-song `.webp` are a
78/// transcode: their bytes depend on quality, lossless, effort, frame-rate, and
79/// width as much as on the source. Folding those into the hash means a settings
80/// change (for example raising the default quality) re-encodes existing covers
81/// on the next run, exactly as a changed source URL does, rather than leaving
82/// them stale. An empty URL still maps to the empty "no artifact" sentinel.
83pub fn webp_art_hash(url: &str, settings: &WebpEncodeSettings) -> String {
84 if url.is_empty() {
85 return String::new();
86 }
87 let mut hasher = fnv::FnvHasher::default();
88 hasher.write(url.as_bytes());
89 hasher.write_u8(0);
90 write_webp_settings(&mut hasher, settings);
91 format!("{:016x}", hasher.finish())
92}
93
94/// Fold the WebP encode `settings` into `hasher` in a fixed field order. Shared
95/// by [`webp_art_hash`] and [`embedded_art_hash`] so the settings contribution
96/// to both sentinels stays bit-identical.
97fn write_webp_settings(hasher: &mut fnv::FnvHasher, settings: &WebpEncodeSettings) {
98 hasher.write_u8(settings.quality);
99 hasher.write_u8(u8::from(settings.lossless));
100 hasher.write_u8(settings.compression_level);
101 hasher.write_u32(settings.max_fps);
102 match settings.max_width {
103 Some(width) => {
104 hasher.write_u8(1);
105 hasher.write_u32(width);
106 }
107 None => hasher.write_u8(0),
108 }
109}
110
111/// The change-detection version for the synced `.lrc` body. Bump this when the
112/// rendered `.lrc` format changes so existing sidecars are rewritten on the next
113/// run (their stored hash then no longer matches, exactly as edited content
114/// would move a [`content_hash`]).
115pub const SYNCED_LRC_VERSION: u32 = 2;
116
117/// A stable per-clip source sentinel for the synced `.lrc` sidecar.
118///
119/// Suno's forced alignment for a given clip is immutable (the audio and its
120/// lyrics are fixed once generated), so the sidecar's rewrite detection keys on
121/// the clip id plus the render [`SYNCED_LRC_VERSION`] rather than the fetched
122/// body. This lets reconcile skip an unchanged clip WITHOUT a network fetch (the
123/// timed body is resolved only when a write is actually planned), while a
124/// version bump rewrites every sidecar. It mirrors how the cover sidecars key on
125/// their source URL rather than the fetched bytes ("the hash tracks the source").
126pub fn synced_lrc_source_hash(clip_id: &str) -> String {
127 content_hash(&format!("synced-lrc/v{SYNCED_LRC_VERSION}/{clip_id}"))
128}
129
130/// A sentinel for the embedded cover art: a digest of the selected art URL, or
131/// the empty string when the clip carries no art. A mismatch against the
132/// manifest means the file on disk holds stale art even if its tags are current.
133pub fn art_hash(clip: &Clip) -> String {
134 art_url_hash(clip.selected_image_url().unwrap_or(""))
135}
136
137/// The embedded-cover sentinel that accounts for the animated-WebP embed.
138///
139/// When `embed_animated` is set and the clip has a `video_cover_url`, the file
140/// embeds a bounded animated WebP, so its identity is the source URL, the encode
141/// `settings`, and the static image URL (folded in because the WebP falls back
142/// to that static JPEG when it will not fit, and a later static-art change must
143/// still re-tag a fallen-back file). It hashes the embed intent, not the runtime
144/// fit outcome, so a pure dry-run cannot know the encoded size and a fit fallback
145/// does not churn. Otherwise (feature off, no preview, or an ALAC target, which
146/// cannot embed WebP) it is the plain static [`art_hash`].
147///
148/// Gating on `video_cover_url` presence scopes an enable-time re-tag to clips
149/// that have a preview rather than the whole library. A preview is an immutable
150/// asset the v3 feed returns intact, so this does not flap, and a stale-preview
151/// fallback is bounded tag churn, never data loss.
152pub fn embedded_art_hash(
153 clip: &Clip,
154 embed_animated: bool,
155 settings: &WebpEncodeSettings,
156) -> String {
157 if embed_animated && !clip.video_cover_url.is_empty() {
158 let mut hasher = fnv::FnvHasher::default();
159 hasher.write(b"webp-embed\0");
160 hasher.write(clip.video_cover_url.as_bytes());
161 hasher.write_u8(0);
162 hasher.write(clip.selected_image_url().unwrap_or("").as_bytes());
163 hasher.write_u8(0);
164 write_webp_settings(&mut hasher, settings);
165 format!("{:016x}", hasher.finish())
166 } else {
167 art_hash(clip)
168 }
169}
170
171#[cfg(test)]
172mod tests {
173 use super::*;
174 use crate::lineage::{EdgeType, ResolveStatus};
175
176 fn sample() -> Clip {
177 Clip {
178 title: "Electric Storm".to_owned(),
179 tags: "ambient, cinematic".to_owned(),
180 image_large_url: "https://cdn1.suno.ai/image_large_abc.jpeg".to_owned(),
181 image_url: "https://cdn1.suno.ai/image_abc.jpeg".to_owned(),
182 video_cover_url: String::new(),
183 prompt: "an orchestral storm".to_owned(),
184 lyrics: "thunder rolls\nover the plains".to_owned(),
185 gpt_description_prompt: "stormy".to_owned(),
186 handle: "alice".to_owned(),
187 display_name: "Alice".to_owned(),
188 ..Default::default()
189 }
190 }
191
192 /// The resolved lineage embedded alongside [`sample`]: an extension of a
193 /// parent under the "Weather Series" root, created in 2023.
194 fn sample_lineage() -> LineageContext {
195 LineageContext {
196 root_id: "root-1".to_owned(),
197 root_title: "Weather Series".to_owned(),
198 root_date: "2023-05-01T00:00:00Z".to_owned(),
199 parent_id: "parent-1".to_owned(),
200 edge_type: Some(EdgeType::Extend),
201 status: ResolveStatus::Resolved,
202 track: 0,
203 track_total: 0,
204 }
205 }
206
207 #[test]
208 fn meta_hash_is_stable() {
209 // Golden value: a change here means the sentinel encoding changed and
210 // every existing manifest would see a spurious retag. Change with care.
211 let h = meta_hash(&sample(), &sample_lineage());
212 assert_eq!(h, "d355c0c463e1f96c");
213 assert_eq!(h.len(), 16);
214 assert_eq!(h, meta_hash(&sample(), &sample_lineage()));
215 }
216
217 #[test]
218 fn art_hash_is_stable_and_empty_without_art() {
219 let h = art_hash(&sample());
220 assert_eq!(h.len(), 16);
221 assert_eq!(h, art_hash(&sample()));
222
223 let mut bare = sample();
224 bare.image_large_url = String::new();
225 bare.image_url = String::new();
226 bare.video_cover_url = String::new();
227 assert_eq!(art_hash(&bare), "");
228 }
229
230 #[test]
231 fn art_url_hash_is_stable_and_empty_for_empty_url() {
232 assert_eq!(art_url_hash(""), "");
233 let h = art_url_hash("https://cdn1.suno.ai/video_cover.mp4");
234 assert_eq!(h.len(), 16);
235 assert_eq!(h, art_url_hash("https://cdn1.suno.ai/video_cover.mp4"));
236 assert_ne!(h, art_url_hash("https://cdn1.suno.ai/other.mp4"));
237 // art_hash routes the selected image URL through the same helper.
238 assert_eq!(
239 art_hash(&sample()),
240 art_url_hash(sample().selected_image_url().unwrap())
241 );
242 }
243
244 #[test]
245 fn webp_art_hash_tracks_url_and_every_encode_setting() {
246 use crate::vocab::WebpEncodeSettings;
247 let url = "https://cdn1.suno.ai/video_cover.mp4";
248 let base = WebpEncodeSettings::default();
249 let h = webp_art_hash(url, &base);
250 assert_eq!(h.len(), 16);
251 assert_eq!(h, webp_art_hash(url, &base), "stable for the same inputs");
252 // Empty URL is the "no artifact" sentinel regardless of settings.
253 assert_eq!(webp_art_hash("", &base), "");
254 // A changed source URL re-transcodes.
255 assert_ne!(h, webp_art_hash("https://cdn1.suno.ai/other.mp4", &base));
256 // Every setting that shapes the output bytes moves the hash, so a
257 // settings change re-encodes an existing cover.
258 for mutate in [
259 |s: &mut WebpEncodeSettings| s.quality = s.quality.wrapping_sub(1),
260 |s: &mut WebpEncodeSettings| s.lossless = !s.lossless,
261 |s: &mut WebpEncodeSettings| s.compression_level = s.compression_level.wrapping_add(1),
262 |s: &mut WebpEncodeSettings| s.max_fps = s.max_fps.wrapping_add(1),
263 |s: &mut WebpEncodeSettings| s.max_width = None,
264 ] {
265 let mut settings = base;
266 mutate(&mut settings);
267 assert_ne!(h, webp_art_hash(url, &settings));
268 }
269 }
270
271 #[test]
272 fn meta_hash_tracks_the_artist_and_model_but_not_sidecar_only_fields() {
273 let lineage = sample_lineage();
274 let base = meta_hash(&sample(), &lineage);
275 // The artist (`display_name`) and model label are embedded tags, so an
276 // upstream change to either must retag.
277 let mut artist = sample();
278 artist.display_name = "Someone Else".to_owned();
279 assert_ne!(meta_hash(&artist, &lineage), base);
280 let mut model = sample();
281 model.model_name = "chirp-v9".to_owned();
282 assert_ne!(meta_hash(&model, &lineage), base);
283 // The animated-cover URL is a sidecar source, not an audio tag: it has
284 // its own hash and must not force a needless audio retag.
285 let mut cover = sample();
286 cover.video_cover_url = "https://cdn1.suno.ai/new_cover.mp4".to_owned();
287 assert_eq!(meta_hash(&cover, &lineage), base);
288 }
289
290 #[test]
291 fn meta_hash_changes_when_a_content_field_changes() {
292 let lineage = sample_lineage();
293 let base = meta_hash(&sample(), &lineage);
294 // Clip-side content fields. (Art lives in `art_hash`, not here.)
295 for mutate in [
296 |c: &mut Clip| c.title = "Different".to_owned(),
297 |c: &mut Clip| c.tags = "lofi".to_owned(),
298 |c: &mut Clip| c.handle = "bob".to_owned(),
299 |c: &mut Clip| c.lyrics = "new words".to_owned(),
300 ] {
301 let mut clip = sample();
302 mutate(&mut clip);
303 assert_ne!(meta_hash(&clip, &lineage), base);
304 }
305 // Resolved-lineage values that get embedded must also move the hash.
306 for mutate in [
307 |l: &mut LineageContext| l.parent_id = "other-parent".to_owned(),
308 |l: &mut LineageContext| l.root_id = "other-root".to_owned(),
309 |l: &mut LineageContext| l.root_title = "Other Album".to_owned(),
310 |l: &mut LineageContext| l.edge_type = Some(EdgeType::Cover),
311 |l: &mut LineageContext| l.root_date = "2099-01-01T00:00:00Z".to_owned(),
312 ] {
313 let mut lin = sample_lineage();
314 mutate(&mut lin);
315 assert_ne!(meta_hash(&sample(), &lin), base);
316 }
317 }
318
319 #[test]
320 fn art_hash_tracks_the_selected_url_in_preference_order() {
321 let mut clip = sample();
322 let large = art_hash(&clip);
323 clip.image_large_url = String::new();
324 let standard = art_hash(&clip);
325 assert_ne!(large, standard);
326 clip.image_url = String::new();
327 clip.video_cover_url = "https://cdn1.suno.ai/video_cover.jpeg".to_owned();
328 let video = art_hash(&clip);
329 assert_ne!(standard, video);
330 }
331
332 #[test]
333 fn content_hash_is_stable_and_tracks_any_change() {
334 let text = "#EXTM3U\n#PLAYLIST:Mix\n#EXTINF:60,One\nA/One.flac\n";
335 let h = content_hash(text);
336 assert_eq!(h.len(), 16);
337 assert_eq!(h, content_hash(text), "same text hashes the same");
338 // A different name, order, path, title, or duration changes the digest.
339 assert_ne!(
340 h,
341 content_hash("#EXTM3U\n#PLAYLIST:Other\n#EXTINF:60,One\nA/One.flac\n")
342 );
343 assert_ne!(
344 h,
345 content_hash("#EXTM3U\n#PLAYLIST:Mix\n#EXTINF:61,One\nA/One.flac\n")
346 );
347 }
348
349 #[test]
350 fn synced_lrc_source_hash_is_stable_per_clip_and_never_empty() {
351 let a = synced_lrc_source_hash("clip-a");
352 assert_eq!(a.len(), 16);
353 assert_eq!(a, synced_lrc_source_hash("clip-a"), "stable per clip id");
354 // Distinct clips get distinct sentinels; none is the empty ("absent")
355 // value, so a desired synced `.lrc` is never mistaken for "no artifact".
356 assert_ne!(a, synced_lrc_source_hash("clip-b"));
357 assert!(!a.is_empty());
358 }
359}