suno-core 0.36.11

Engine for a download-only Suno.ai library tool: feed selection, sync reconciliation, and audio tagging.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
use super::*;

#[test]
fn stem_path_drift_emits_move() {
    // #141: a stem whose path drifts at an equal hash is relocated with a
    // rename rather than re-rendered or re-fetched.
    let mut manifest = Manifest::new();
    manifest.insert(
        "a",
        entry_with_stems("a", &[("voc", "old.stems/voc.mp3", "h1")]),
    );
    let d = vec![stem_desired(
        "a",
        Some(vec![dstem("voc", "new.stems/voc.mp3", "h1")]),
    )];
    let local: HashMap<String, LocalFile> = [
        ("a".to_string(), present(100)),
        ("old.stems/voc.mp3".to_string(), present(50)),
    ]
    .into_iter()
    .collect();
    let plan = reconcile(&manifest, &d, &local, &mirror_ok());
    assert_eq!(plan.stem_moves(), 1);
    assert_eq!(plan.stem_writes(), 0);
    assert!(plan.actions.contains(&Action::MoveStem {
        clip_id: "a".to_string(),
        key: "voc".to_string(),
        stem_id: "voc".to_string(),
        from: "old.stems/voc.mp3".to_string(),
        to: "new.stems/voc.mp3".to_string(),
        source_url: "https://cdn1.suno.ai/voc.mp3".to_string(),
        format: StemFormat::Mp3,
        hash: "h1".to_string(),
    }));
}

fn dstem(key: &str, path: &str, hash: &str) -> DesiredStem {
    DesiredStem {
        key: key.to_string(),
        stem_id: key.to_string(),
        path: path.to_string(),
        source_url: format!("https://cdn1.suno.ai/{key}.mp3"),
        format: StemFormat::Mp3,
        hash: hash.to_string(),
    }
}

/// A kept FLAC clip that desires the given (possibly `None`) stem set.
fn stem_desired(id: &str, stems: Option<Vec<DesiredStem>>) -> Desired {
    Desired {
        stems,
        ..desired(id, &format!("{id}.flac"), AudioFormat::Flac, "m", "art")
    }
}

/// A manifest entry for a kept clip carrying the given tracked stems.
fn entry_with_stems(id: &str, stems: &[(&str, &str, &str)]) -> ManifestEntry {
    let mut e = entry(&format!("{id}.flac"), AudioFormat::Flac, "m", "art");
    for (key, path, hash) in stems {
        e.stems.insert(
            key.to_string(),
            ArtifactState {
                path: path.to_string(),
                hash: hash.to_string(),
            },
        );
    }
    e
}

fn stem_writes(plan: &Plan) -> Vec<(&str, &str)> {
    plan.actions
        .iter()
        .filter_map(|a| match a {
            Action::WriteStem { key, path, .. } => Some((key.as_str(), path.as_str())),
            _ => None,
        })
        .collect()
}

fn stem_deletes(plan: &Plan) -> Vec<(&str, &str)> {
    plan.actions
        .iter()
        .filter_map(|a| match a {
            Action::DeleteStem { key, path, .. } => Some((key.as_str(), path.as_str())),
            _ => None,
        })
        .collect()
}

#[test]
fn stems_none_keeps_every_existing_stem() {
    // An indeterminate listing (feature off, has_stem false, or a
    // paged-error) surfaces as `None`: no stem is written or deleted.
    let mut manifest = Manifest::new();
    manifest.insert(
        "a",
        entry_with_stems(
            "a",
            &[
                ("voc", "a.stems/voc.mp3", "h1"),
                ("drm", "a.stems/drm.mp3", "h2"),
            ],
        ),
    );
    let d = vec![stem_desired("a", None)];
    let plan = reconcile(&manifest, &d, &local_present("a"), &mirror_ok());
    assert_eq!(plan.stem_writes(), 0);
    assert_eq!(plan.stem_deletes(), 0);
}

#[test]
fn stems_authoritative_writes_missing_stems() {
    let mut manifest = Manifest::new();
    manifest.insert("a", entry("a.flac", AudioFormat::Flac, "m", "art"));
    let d = vec![stem_desired(
        "a",
        Some(vec![
            dstem("voc", "a.stems/voc.mp3", "h1"),
            dstem("drm", "a.stems/drm.mp3", "h2"),
        ]),
    )];
    let plan = reconcile(&manifest, &d, &local_present("a"), &mirror_ok());
    assert_eq!(
        stem_writes(&plan),
        vec![("voc", "a.stems/voc.mp3"), ("drm", "a.stems/drm.mp3")]
    );
    assert_eq!(plan.stem_deletes(), 0);
}

#[test]
fn stems_authoritative_rewrites_only_on_hash_or_path_drift() {
    let mut manifest = Manifest::new();
    // voc unchanged, drm hash drift, bas path drift (song moved).
    manifest.insert(
        "a",
        entry_with_stems(
            "a",
            &[
                ("voc", "a.stems/voc.mp3", "h1"),
                ("drm", "a.stems/drm.mp3", "h2"),
                ("bas", "old.stems/bas.mp3", "h3"),
            ],
        ),
    );
    let d = vec![stem_desired(
        "a",
        Some(vec![
            dstem("voc", "a.stems/voc.mp3", "h1"),     // unchanged
            dstem("drm", "a.stems/drm.mp3", "h2-new"), // hash drift
            dstem("bas", "a.stems/bas.mp3", "h3"),     // path drift
        ]),
    )];
    let plan = reconcile(&manifest, &d, &local_present("a"), &mirror_ok());
    assert_eq!(
        stem_writes(&plan),
        vec![("drm", "a.stems/drm.mp3"), ("bas", "a.stems/bas.mp3")]
    );
    assert_eq!(plan.stem_deletes(), 0);
}

#[test]
fn stems_authoritative_removes_a_stem_absent_from_the_set() {
    // drm is gone from the authoritative listing, so it is delete-reconciled
    // through the shared gate; voc (still present) is untouched.
    let mut manifest = Manifest::new();
    manifest.insert(
        "a",
        entry_with_stems(
            "a",
            &[
                ("voc", "a.stems/voc.mp3", "h1"),
                ("drm", "a.stems/drm.mp3", "h2"),
            ],
        ),
    );
    let d = vec![stem_desired(
        "a",
        Some(vec![dstem("voc", "a.stems/voc.mp3", "h1")]),
    )];
    let plan = reconcile(&manifest, &d, &local_present("a"), &mirror_ok());
    assert_eq!(plan.stem_writes(), 0);
    assert_eq!(stem_deletes(&plan), vec![("drm", "a.stems/drm.mp3")]);
}

#[test]
fn stems_removal_needs_deletion_allowed() {
    // The same authoritative-omission case, but deletion is not allowed this
    // run (no fully-enumerated mirror). The stem is KEPT, never deleted.
    let mut manifest = Manifest::new();
    manifest.insert(
        "a",
        entry_with_stems(
            "a",
            &[
                ("voc", "a.stems/voc.mp3", "h1"),
                ("drm", "a.stems/drm.mp3", "h2"),
            ],
        ),
    );
    let d = vec![stem_desired(
        "a",
        Some(vec![dstem("voc", "a.stems/voc.mp3", "h1")]),
    )];

    let incomplete = vec![SourceStatus {
        mode: SourceMode::Mirror,
        fully_enumerated: false,
    }];
    assert_eq!(
        reconcile(&manifest, &d, &local_present("a"), &incomplete).stem_deletes(),
        0
    );

    let copy_only = vec![SourceStatus {
        mode: SourceMode::Copy,
        fully_enumerated: true,
    }];
    assert_eq!(
        reconcile(&manifest, &d, &local_present("a"), &copy_only).stem_deletes(),
        0
    );
}

#[test]
fn stems_removal_skipped_for_preserved_or_protected_clip() {
    let mut manifest = Manifest::new();
    let mut e = entry_with_stems(
        "a",
        &[
            ("voc", "a.stems/voc.mp3", "h1"),
            ("drm", "a.stems/drm.mp3", "h2"),
        ],
    );
    e.preserve = true;
    manifest.insert("a", e);
    let authoritative = Some(vec![dstem("voc", "a.stems/voc.mp3", "h1")]);

    // preserve marker wins: no stem delete.
    let d = vec![stem_desired("a", authoritative.clone())];
    assert_eq!(
        reconcile(&manifest, &d, &local_present("a"), &mirror_ok()).stem_deletes(),
        0
    );

    // A copy-held clip this run also keeps all stems (protected_now).
    let mut manifest2 = Manifest::new();
    manifest2.insert(
        "a",
        entry_with_stems(
            "a",
            &[
                ("voc", "a.stems/voc.mp3", "h1"),
                ("drm", "a.stems/drm.mp3", "h2"),
            ],
        ),
    );
    let held = Desired {
        modes: vec![SourceMode::Mirror, SourceMode::Copy],
        stems: authoritative,
        ..desired("a", "a.flac", AudioFormat::Flac, "m", "art")
    };
    assert_eq!(
        reconcile(&manifest2, &[held], &local_present("a"), &mirror_ok()).stem_deletes(),
        0
    );
}

#[test]
fn stems_are_co_deleted_when_the_song_is_trashed() {
    // A trashed clip's audio is deleted; its stems must be co-deleted so the
    // `.stems` folder is not orphaned (no stranding).
    let mut manifest = Manifest::new();
    manifest.insert(
        "a",
        entry_with_stems(
            "a",
            &[
                ("voc", "a.stems/voc.mp3", "h1"),
                ("drm", "a.stems/drm.mp3", "h2"),
            ],
        ),
    );
    let trashed = Desired {
        trashed: true,
        ..desired("a", "a.flac", AudioFormat::Flac, "m", "art")
    };
    let plan = reconcile(&manifest, &[trashed], &local_present("a"), &mirror_ok());
    assert_eq!(plan.deletes(), 1, "the trashed audio is deleted");
    let mut deleted: Vec<&str> = stem_deletes(&plan).into_iter().map(|(k, _)| k).collect();
    deleted.sort_unstable();
    assert_eq!(deleted, vec!["drm", "voc"], "both stems co-deleted");
}

#[test]
fn stems_are_co_deleted_for_an_absent_clip() {
    let mut manifest = Manifest::new();
    manifest.insert(
        "a",
        entry_with_stems("a", &[("voc", "a.stems/voc.mp3", "h1")]),
    );
    // Desired is empty: clip "a" left every source and is deleted.
    let plan = reconcile(&manifest, &[], &local_present("a"), &mirror_ok());
    assert_eq!(plan.deletes(), 1);
    assert_eq!(stem_deletes(&plan), vec![("voc", "a.stems/voc.mp3")]);
}

#[test]
fn stems_are_kept_when_absent_clip_listing_is_incomplete() {
    // SYNC-9: an unreliable listing deletes nothing, stems included.
    let mut manifest = Manifest::new();
    manifest.insert(
        "a",
        entry_with_stems("a", &[("voc", "a.stems/voc.mp3", "h1")]),
    );
    let incomplete = vec![SourceStatus {
        mode: SourceMode::Mirror,
        fully_enumerated: false,
    }];
    let plan = reconcile(&manifest, &[], &HashMap::new(), &incomplete);
    assert_eq!(plan.deletes(), 0);
    assert_eq!(plan.stem_deletes(), 0);
}

#[test]
fn stem_delete_is_suppressed_when_it_aliases_a_stem_write() {
    // A prior stem at a path is being removed, while a different stem is
    // written to that same path this run (a re-key at a stable path). The
    // delete must be downgraded so it can never clobber the fresh write.
    let mut manifest = Manifest::new();
    manifest.insert(
        "a",
        entry_with_stems("a", &[("old", "a.stems/mix.mp3", "h1")]),
    );
    let d = vec![stem_desired(
        "a",
        Some(vec![dstem("new", "a.stems/mix.mp3", "h2")]),
    )];
    let plan = reconcile(&manifest, &d, &local_present("a"), &mirror_ok());
    // The new stem is written to the shared path; the old key's delete of the
    // same path is suppressed (no DeleteStem survives for that path).
    assert_eq!(stem_writes(&plan), vec![("new", "a.stems/mix.mp3")]);
    assert!(
        !plan.actions.iter().any(|a| matches!(
            a,
            Action::DeleteStem { path, .. } if path == "a.stems/mix.mp3"
        )),
        "a stem delete must never alias a stem write target"
    );
}

// #355: with no authoritative stem listing (`d.stems == None`), a retitle (or a
// pre-fix strand) relocates every tracked stem to the current `.stems` folder
// with a folder-only reparent, emitting only MoveStem and never a delete.

/// A kept FLAC entry at `audio` carrying the given tracked stems.
fn stems_entry_at(audio: &str, stems: &[(&str, &str, &str)]) -> ManifestEntry {
    let mut e = entry(audio, AudioFormat::Flac, "m", "art");
    for (key, path, hash) in stems {
        e.stems.insert(
            key.to_string(),
            ArtifactState {
                path: path.to_string(),
                hash: hash.to_string(),
            },
        );
    }
    e
}

/// A kept clip whose audio drifts to `new_path`, with no authoritative stems.
fn stems_none_at(id: &str, new_path: &str) -> Desired {
    Desired {
        stems: None,
        ..desired(id, new_path, AudioFormat::Flac, "m", "art")
    }
}

#[test]
fn stems_none_relocates_stranded_stems_on_retitle() {
    let mut manifest = Manifest::new();
    manifest.insert(
        "a",
        stems_entry_at(
            "Old.flac",
            &[
                ("voc", "Old.stems/voc.mp3", "h1"),
                ("drm", "Old.stems/drm.mp3", "h2"),
            ],
        ),
    );
    let d = vec![stems_none_at("a", "New.flac")];
    let local: HashMap<String, LocalFile> = [
        ("a".to_string(), present(100)),
        ("Old.stems/voc.mp3".to_string(), present(50)),
        ("Old.stems/drm.mp3".to_string(), present(50)),
    ]
    .into_iter()
    .collect();
    let plan = reconcile(&manifest, &d, &local, &mirror_ok());
    assert_eq!(plan.stem_moves(), 2);
    assert_eq!(plan.stem_writes(), 0);
    assert_eq!(plan.stem_deletes(), 0);
    assert!(plan.actions.contains(&Action::MoveStem {
        clip_id: "a".to_string(),
        key: "voc".to_string(),
        stem_id: String::new(),
        from: "Old.stems/voc.mp3".to_string(),
        to: "New.stems/voc.mp3".to_string(),
        source_url: String::new(),
        format: StemFormat::Mp3,
        hash: "h1".to_string(),
    }));
    assert!(plan.actions.contains(&Action::MoveStem {
        clip_id: "a".to_string(),
        key: "drm".to_string(),
        stem_id: String::new(),
        from: "Old.stems/drm.mp3".to_string(),
        to: "New.stems/drm.mp3".to_string(),
        source_url: String::new(),
        format: StemFormat::Mp3,
        hash: "h2".to_string(),
    }));
}

#[test]
fn stems_none_relocation_skipped_when_old_stem_absent() {
    // No source_url or stem_id is known, so a vanished stem cannot be
    // re-rendered: skip cleanly, never write, never delete.
    let mut manifest = Manifest::new();
    manifest.insert(
        "a",
        stems_entry_at("Old.flac", &[("voc", "Old.stems/voc.mp3", "h1")]),
    );
    let d = vec![stems_none_at("a", "New.flac")];
    // Only the audio is present on disk.
    let plan = reconcile(&manifest, &d, &local_present("a"), &mirror_ok());
    assert_eq!(plan.stem_moves(), 0);
    assert_eq!(plan.stem_writes(), 0);
    assert_eq!(plan.stem_deletes(), 0);
}

#[test]
fn stems_none_no_relocation_without_retitle() {
    // Stems already sit at the current base and the audio is stable: idempotent,
    // no move. Complements `stems_none_keeps_every_existing_stem`.
    let mut manifest = Manifest::new();
    manifest.insert(
        "a",
        stems_entry_at("a.flac", &[("voc", "a.stems/voc.mp3", "h1")]),
    );
    let d = vec![stem_desired("a", None)];
    let local: HashMap<String, LocalFile> = [
        ("a".to_string(), present(100)),
        ("a.stems/voc.mp3".to_string(), present(50)),
    ]
    .into_iter()
    .collect();
    let plan = reconcile(&manifest, &d, &local, &mirror_ok());
    assert_eq!(plan.stem_moves(), 0);
    assert_eq!(plan.stem_writes(), 0);
    assert_eq!(plan.stem_deletes(), 0);
}

#[test]
fn stems_none_not_relocated_for_trashed_clip() {
    // A trashed clip kept in place (delete gate refuses) with a drifted d.path
    // must NOT have its stems relocated to the new base (strand inversion).
    let mut manifest = Manifest::new();
    manifest.insert(
        "a",
        stems_entry_at("Old.flac", &[("voc", "Old.stems/voc.mp3", "h1")]),
    );
    let d = vec![Desired {
        trashed: true,
        ..stems_none_at("a", "New.flac")
    }];
    let local: HashMap<String, LocalFile> = [
        ("a".to_string(), present(100)),
        ("Old.stems/voc.mp3".to_string(), present(50)),
    ]
    .into_iter()
    .collect();
    let not_enumerated = vec![SourceStatus {
        mode: SourceMode::Mirror,
        fully_enumerated: false,
    }];
    let plan = reconcile(&manifest, &d, &local, &not_enumerated);
    assert_eq!(plan.stem_moves(), 0, "no strand inversion");
    assert_eq!(plan.stem_deletes(), 0);
    assert_eq!(plan.deletes(), 0);
}

#[test]
fn two_clip_title_swap_with_stems_is_clobber_safe() {
    // Clips a and b swap titles, each with a tracked stem. a's stem reparents
    // onto b's held stem path and vice versa; the clobber backstop suppresses
    // both, so no `.stems` file is targeted onto another live clip's path and
    // none is lost.
    let mut manifest = Manifest::new();
    manifest.insert(
        "a",
        stems_entry_at("A.flac", &[("voc", "A.stems/voc.mp3", "h1")]),
    );
    manifest.insert(
        "b",
        stems_entry_at("B.flac", &[("voc", "B.stems/voc.mp3", "h2")]),
    );
    // a takes B's base, b takes A's base.
    let d = vec![stems_none_at("a", "B.flac"), stems_none_at("b", "A.flac")];
    let local: HashMap<String, LocalFile> = [
        ("a".to_string(), present(100)),
        ("b".to_string(), present(100)),
        ("A.stems/voc.mp3".to_string(), present(50)),
        ("B.stems/voc.mp3".to_string(), present(50)),
    ]
    .into_iter()
    .collect();
    let plan = reconcile(&manifest, &d, &local, &mirror_ok());
    assert_eq!(
        plan.stem_moves(),
        0,
        "both colliding stem moves are suppressed to Skip"
    );
    assert_eq!(plan.stem_deletes(), 0, "no stem is lost");
}