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