File size: 69,968 Bytes
e880b2b
29c3be9
 
 
 
 
 
 
e880b2b
29c3be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
607c4f0
 
 
 
 
ca4ec99
ea9092c
6049960
 
 
 
 
607c4f0
29c3be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca4ec99
 
 
 
29c3be9
 
 
 
ea9092c
 
 
 
e880b2b
 
 
 
 
 
 
29c3be9
 
 
 
 
 
607c4f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
536b543
 
 
c871492
 
607c4f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6049960
 
 
 
 
 
 
 
 
29c3be9
 
6049960
 
29c3be9
 
6049960
29c3be9
 
6049960
 
 
 
 
 
 
 
29c3be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
607c4f0
ca4ec99
e880b2b
29c3be9
 
 
 
ca4ec99
 
 
 
 
 
 
 
 
 
 
 
 
ea9092c
 
ca4ec99
e880b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca4ec99
 
 
ea9092c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6049960
ea9092c
6049960
ea9092c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e880b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca4ec99
 
 
 
 
 
 
 
 
 
 
 
6049960
ca4ec99
6049960
ca4ec99
 
 
 
 
 
 
 
 
 
607c4f0
 
 
 
ca4ec99
 
 
 
607c4f0
 
ca4ec99
607c4f0
 
 
 
 
 
 
 
 
6049960
607c4f0
6049960
607c4f0
 
 
 
 
 
ca4ec99
 
 
 
 
607c4f0
 
 
 
 
 
 
 
 
 
 
 
29c3be9
 
 
 
e880b2b
 
29c3be9
 
 
 
 
 
 
 
 
e880b2b
 
29c3be9
ca4ec99
29c3be9
 
 
 
 
 
 
 
 
 
 
 
607c4f0
 
 
 
 
 
29c3be9
 
 
 
 
 
 
 
 
 
 
 
6049960
29c3be9
6049960
29c3be9
607c4f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6049960
607c4f0
6049960
607c4f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca4ec99
607c4f0
ca4ec99
 
607c4f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6049960
607c4f0
6049960
607c4f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca4ec99
 
607c4f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6049960
607c4f0
6049960
607c4f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29c3be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca4ec99
 
 
 
ea9092c
 
ca4ec99
29c3be9
ca4ec99
29c3be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
607c4f0
 
 
 
 
 
 
 
 
 
 
 
29c3be9
 
607c4f0
 
 
6049960
 
e880b2b
29c3be9
 
 
 
 
6049960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29c3be9
 
 
 
 
 
 
 
 
 
e880b2b
29c3be9
 
 
e880b2b
6049960
29c3be9
 
 
 
 
 
 
 
 
 
 
ea9092c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29c3be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e880b2b
 
 
 
29c3be9
 
 
 
 
 
 
6049960
29c3be9
 
6049960
 
 
 
29c3be9
607c4f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea9092c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
607c4f0
 
 
 
 
 
 
ca4ec99
 
 
607c4f0
ca4ec99
607c4f0
 
 
 
 
 
ca4ec99
 
 
 
 
 
 
 
 
607c4f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29c3be9
 
 
ca4ec99
 
29c3be9
 
 
 
 
 
 
 
 
 
ca4ec99
 
29c3be9
 
ca4ec99
29c3be9
 
 
 
 
 
 
 
 
ca4ec99
 
 
29c3be9
 
 
 
 
 
 
ca4ec99
 
 
29c3be9
 
e880b2b
29c3be9
 
 
 
 
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
"""Local-first FastAPI server for Tune-Whisperer (internal: ABSOLUTION Ω) v1.1.

No remote calls are made by this application. Jobs are process-local and not
durable; no public multi-user deployment without authorization is supported.
"""
from __future__ import annotations

import json
import importlib.util
import os
import threading
import uuid
import zipfile
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
from pathlib import Path
from typing import Literal

import numpy as np
import soundfile as sf
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
from fastapi.responses import FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field

from .bass import BassParams, TRACKS, process_bass_file
from .noise import RepairParams, inspect_noise_file, repair_noise_file
from .virtualbass import available as virtualbass_available, render_virtualbass
from .mini_model import inspect_file as inspect_mini, MODEL_FILE
from .mixer import render_mix, sampled_peaks, rubberband_available
from .unified import build_unified_graph
from .omnistem import compile_plan, execute_plan
from . import access
import contextvars, secrets as _secrets
from http.cookies import SimpleCookie
from fastapi import Request
from fastapi.responses import RedirectResponse

from .audio import (
    AudioError, align_and_resample, analyze_file, hpss_separate_file,
    load_audio, mixture_report, save_stems, sha256_file,
)

# Byte quota protects local disk; no maximum duration is imposed on audio.
# Configure ABSOLUTION_MAX_UPLOAD_MIB to fit your available storage.
MAX_UPLOAD_MIB = max(1, min(4096, int(os.environ.get("ABSOLUTION_MAX_UPLOAD_MIB", "1024"))))
MAX_UPLOAD_BYTES = MAX_UPLOAD_MIB * 1024 * 1024
ALLOWED_EXTENSIONS = {".wav", ".flac", ".aif", ".aiff", ".mp3", ".ogg"}


def timestamp() -> str:
    return datetime.now(timezone.utc).isoformat(timespec="seconds")


def check_id(raw: str) -> str:
    try:
        return str(uuid.UUID(raw))
    except ValueError as exc:
        raise HTTPException(status_code=404, detail="Project not found.") from exc


# Demucs on shared CPU hardware: cap input duration (0 = unlimited).
DEMUCS_MAX_SECONDS = float(os.environ.get("ABSOLUTION_DEMUCS_MAX_SECONDS", "0"))


class ProjectCreate(BaseModel):
    name: str = Field(default="Untitled project", min_length=1, max_length=80)


class ProjectRename(BaseModel):
    name: str = Field(min_length=1, max_length=80)


class FusionRequest(BaseModel):
    project_id: str
    mode: Literal["auto", "neural", "dsp"] = "auto"
    include_event_proxies: bool = True
    scan_noise: bool = True


class SeparationRequest(BaseModel):
    project_id: str
    model: Literal["hpss", "demucs"] = "hpss"
    target_description: str = Field(default="", max_length=300)


class BassRequest(BaseModel):
    project_id: str
    source_mode: Literal["auto", "dsp", "hpss", "demucs"] = "auto"
    crossover_hz: float = Field(default=220, ge=45, le=350)
    kick_focus: float = Field(default=0.75, ge=0, le=1)
    kick_gain_db: float = Field(default=0, ge=-12, le=12)
    bass_gain_db: float = Field(default=0, ge=-12, le=12)
    sub_gain_db: float = Field(default=0, ge=-12, le=12)
    harmonics: float = Field(default=0.20, ge=0, le=1)
    lowcut_hz: float = Field(default=22, ge=10, le=45)
    mono_below_hz: float = Field(default=100, ge=0, le=180)
    wet: float = Field(default=1, ge=0, le=1)
    ceiling_dbfs: float = Field(default=-1, ge=-6, le=-0.1)


class NoiseScanRequest(BaseModel):
    project_id: str


class NoiseRepairRequest(BaseModel):
    project_id: str
    approved_samples: list[int] = Field(default_factory=list, max_length=400)
    strength: float = Field(default=0.8, ge=0, le=1)



class VirtualBassRequest(BaseModel):
    project_id: str
    cutoff_hz: float = Field(default=250, ge=100, le=500)
    strength: float = Field(default=1.0, ge=0.5, le=3.0)



class TrackMix(BaseModel):
    gain_db: float = Field(default=0, ge=-48, le=12)
    pan: float = Field(default=0, ge=-1, le=1)
    mute: bool = False
    solo: bool = False
    highpass_hz: float = Field(default=0, ge=0, le=400)
    lowpass_hz: float = Field(default=0, ge=0, le=18000)
    width: float = Field(default=1, ge=0, le=2)
    drive: float = Field(default=0, ge=0, le=1)
    delay_ms: float = Field(default=0, ge=0, le=280)
    delay_mix: float = Field(default=0, ge=0, le=0.55)
    room: float = Field(default=0, ge=0, le=0.45)


class MixerRequest(BaseModel):
    project_id: str
    tracks: dict[str, TrackMix] = Field(default_factory=dict)
    tempo: float = Field(default=1, ge=0.5, le=2)
    pitch_semitones: float = Field(default=0, ge=-12, le=12)


class PlannedTrack(BaseModel):
    id: str = Field(min_length=1, max_length=60, pattern=r"^[a-z0-9_-]+$")
    label: str = Field(min_length=1, max_length=100)
    enabled: bool = True


class PlanRequest(BaseModel):
    tracks: list[PlannedTrack] = Field(max_length=128)


class _ContextExecutor:
    """Runs jobs inside the submitting request's context so they keep its namespace."""
    def __init__(self, inner):
        self.inner = inner
    def submit(self, fn, *args, **kwargs):
        ctx = contextvars.copy_context()
        return self.inner.submit(ctx.run, fn, *args, **kwargs)


class Service:
    def __init__(self, data_root: Path):
        self.base_root = data_root.resolve()
        self.base_root.mkdir(parents=True, exist_ok=True)
        self.lock = threading.RLock()
        self.jobs: dict[str, dict] = {}
        self.executor = _ContextExecutor(ThreadPoolExecutor(max_workers=2, thread_name_prefix="omega"))
        self.model_repo = os.environ.get("ABSOLUTION_DEMUCS_REPO", "").strip()

    @property
    def data_root(self) -> Path:
        """Namespace root of the current request (owner root or guests/<ns>). Fails closed."""
        root = access.current_root.get()
        if root is None:
            raise HTTPException(status_code=403, detail="No session namespace.")
        return root

    def folder(self, project_id: str) -> Path:
        project_id = check_id(project_id)
        folder = self.data_root / project_id
        if not (folder / "manifest.json").is_file():
            raise HTTPException(status_code=404, detail="Project not found.")
        return folder

    def manifest(self, project_id: str) -> dict:
        return json.loads((self.folder(project_id) / "manifest.json").read_text(encoding="utf-8"))

    def write_manifest(self, project_id: str, manifest: dict) -> None:
        folder = self.data_root / check_id(project_id)
        target = folder / "manifest.json"
        temp = folder / f".{uuid.uuid4().hex}.tmp"
        temp.write_text(json.dumps(manifest, indent=2, ensure_ascii=False), encoding="utf-8")
        temp.replace(target)

    def create_project(self, name: str) -> dict:
        project_id = str(uuid.uuid4())
        folder = self.data_root / project_id
        folder.mkdir()
        manifest = {
            "id": project_id, "name": name, "created_at": timestamp(),
            "asset": None, "analysis": None, "stems": {},
            "separation": None, "report": None, "bass": None, "noise": None, "virtualbass": None,
            "inventory": None, "plan": [], "mixer": None, "unified": None,
            "omnistem": None, "omnistem_base": None, "fusion": None,
        }
        self.write_manifest(project_id, manifest)
        return manifest

    def active_tracks(self, manifest: dict) -> dict:
        graph = manifest.get("unified")
        if graph and graph.get("mix_consistent"):
            return graph["stems"]
        return manifest.get("stems") or {}

    def refresh_unified(self, project_id: str) -> dict | None:
        """Called only from worker or explicit user action. Never overwrites source."""
        manifest = self.manifest(project_id)
        graph = build_unified_graph(self.folder(project_id), manifest)
        with self.lock:
            latest = self.manifest(project_id)
            latest["unified"] = graph
            latest["omnistem"] = None
            latest["omnistem_base"] = None
            latest["mixer"] = None
            # Keep the provenance display in sync when a user later approves
            # a click repair (which adds a genuine difference lane).
            if latest.get("fusion"):
                fusion = latest["fusion"]
                current = graph["stems"] if graph and graph.get("mix_consistent") else latest.get("stems", {})
                previous = fusion.get("stems") or {}
                refreshed = {}
                for name, item in current.items():
                    meta = previous.get(name, {})
                    parent = (
                        "Manuell freigegebene Klick-Reparatur des Originals"
                        if name == "noise_difference" else
                        "HPSS Percussive-Stem" if name == "kick_core" and fusion["method"] == "hpss" else
                        "HPSS Harmonic-Stem" if name == "bassline_core" and fusion["method"] == "hpss" else
                        "Demucs Drums-Stem" if name == "kick_core" else
                        "Demucs Bass-Stem" if name == "bassline_core" else
                        meta.get("derived_from", "Originalmix / bisherige Separation")
                    )
                    refreshed[name] = {
                        "label": item.get("label", name),
                        "role": item.get("role", name),
                        "derived_from": parent,
                        "type": (
                            "approved_repair" if name == "noise_difference" else
                            "dsp_estimate" if name in ("kick_core", "bassline_core", "snare_proxy", "hat_proxy") else
                            "neural" if fusion["method"] == "demucs" and name in ("vocals", "drums", "bass", "other") else
                            "signal_component"
                        ),
                    }
                fusion["stems"] = refreshed
                fusion["mixture_consistent"] = bool(graph and graph.get("mix_consistent"))
                fusion["noise_candidates"] = len(
                    ((latest.get("noise") or {}).get("scan") or {}).get("events") or []
                )
                fusion["warnings"] = [
                    note for note in fusion.get("warnings", [])
                    if not ("Störspur" in note)
                ] if "noise_difference" in refreshed else fusion.get("warnings", [])
                fusion["updated_at"] = timestamp()
            self.write_manifest(project_id, latest)
        return graph

    def create_omnistem_job(self, project_id: str) -> dict:
        project_id = check_id(project_id)
        with self.lock:
            manifest = self.manifest(project_id)
            if not manifest.get("asset"):
                raise HTTPException(status_code=409, detail="Bitte Audio importieren.")
            if not manifest.get("stems"):
                raise HTTPException(status_code=409, detail="Zuerst HPSS oder Demucs separieren.")
            if any(j["project_id"] == project_id and j["status"] in ("queued","running")
                   for j in self.jobs.values()):
                raise HTTPException(status_code=409, detail="Projekt wird bereits verarbeitet.")
            job = {"id": str(uuid.uuid4()), "project_id": project_id,
                   "kind": "omnistem", "model": "explicit_plan_dsp_proxy_v1",
                   "status": "queued", "progress": 0,
                   "created_at": timestamp(), "error": None}
            job["ns_root"] = str(self.data_root); self.jobs[job["id"]] = job
            self.executor.submit(self.run_omnistem_job, job["id"])
            return {k: v for k, v in job.items() if k != "ns_root"}

    def run_omnistem_job(self, job_id: str) -> None:
        pid = self.jobs[job_id]["project_id"]
        try:
            self.update_job(job_id, status="running", progress=5)
            manifest = self.manifest(pid)
            source_graph = manifest.get("unified")
            if source_graph and source_graph.get("mode") == "omnistem_plan_v1":
                source_graph = manifest.get("omnistem_base")
            newgraph, report = execute_plan(
                self.folder(pid), manifest, manifest.get("plan") or [],
                progress=lambda f: self.update_job(job_id, progress=5+int(90*f)),
            )
            with self.lock:
                updated = self.manifest(pid)
                updated["omnistem_base"] = source_graph
                updated["omnistem"] = {
                    "id": job_id, "completed_at": timestamp(),
                    "source_sha256": updated["asset"]["sha256"], "compiled": report,
                    "note": "No neural instrument isolation. Proxies are estimates.",
                }
                updated["unified"] = newgraph
                updated["mixer"] = None
                self.write_manifest(pid, updated)
            self.update_job(job_id, status="done", progress=100)
        except Exception as exc:
            self.update_job(job_id, status="failed", error=f"{type(exc).__name__}: {exc}")

    def create_fusion_job(self, request: FusionRequest) -> dict:
        """Single user operation; each internal stage is sequential and real."""
        pid = check_id(request.project_id)
        with self.lock:
            manifest = self.manifest(pid)
            if not manifest.get("asset"):
                raise HTTPException(status_code=409, detail="Zuerst eine Audiodatei importieren.")
            if any(j["project_id"] == pid and j["status"] in ("queued", "running")
                   for j in self.jobs.values()):
                raise HTTPException(status_code=409, detail="Projekt wird gerade verarbeitet.")
            neural_available = self.model_info()[1]["ready"]
            if request.mode == "neural" and not neural_available:
                raise HTTPException(
                    status_code=409,
                    detail="Echte Vocals-/Bass-/Drums-KI braucht lokale Demucs-Gewichte."
                )
            resolved = "demucs" if neural_available and request.mode != "dsp" else "hpss"
            fallback_note = None
            if resolved == "demucs" and DEMUCS_MAX_SECONDS > 0:
                duration = float((manifest.get("analysis") or {}).get("duration_seconds") or 0)
                if duration > DEMUCS_MAX_SECONDS:
                    if request.mode == "neural":
                        raise HTTPException(status_code=413, detail=(
                            f"Demucs ist auf dieser CPU-Instanz auf {DEMUCS_MAX_SECONDS:g} s begrenzt "
                            f"(Datei: {duration:.0f} s). Bitte DSP-Modus wählen oder lokal arbeiten."))
                    resolved = "hpss"
                    fallback_note = (f"Demucs übersprungen: Datei {duration:.0f} s > {DEMUCS_MAX_SECONDS:g} s Limit "
                                     "dieser Instanz; HPSS verwendet.")
            job = {
                "id": str(uuid.uuid4()), "project_id": pid, "kind": "fusion",
                "model": resolved, "status": "queued", "progress": 0,
                "stage": "Warte auf Verarbeitung", "created_at": timestamp(),
                "error": None,
            }
            if fallback_note:
                job["fallback_note"] = fallback_note
            job["ns_root"] = str(self.data_root); self.jobs[job["id"]] = job
            self.executor.submit(self.run_fusion_job, job["id"], request.model_dump())
            return {k: v for k, v in job.items() if k != "ns_root"}

    def _fusion_stage(self, parent_id: str, stage: str, progress: int,
                      method, *args) -> None:
        """Execute an existing validated worker in the SAME processing task.

        No parallel processing, no second input, no hidden gain changes.
        """
        pid = self.jobs[parent_id]["project_id"]
        child = str(uuid.uuid4())
        self.jobs[child] = {
            "id": child, "project_id": pid, "kind": "fusion_internal",
            "model": self.jobs[parent_id]["model"], "status": "running", "progress": 0, "error": None,
        }
        self.update_job(parent_id, progress=progress, stage=stage)
        try:
            method(child, *args)
            result = self.jobs[child]
            if result["status"] != "done":
                raise RuntimeError(result.get("error") or stage + " fehlgeschlagen")
        finally:
            # Internal implementation details are not independent user jobs.
            with self.lock:
                self.jobs.pop(child, None)

    def run_fusion_job(self, job_id: str, options: dict) -> None:
        pid = self.jobs[job_id]["project_id"]
        method = self.jobs[job_id]["model"]
        warnings: list[str] = []
        if self.jobs[job_id].get("fallback_note"):
            warnings.append(self.jobs[job_id]["fallback_note"])
        stages: list[str] = []
        try:
            self.update_job(job_id, status="running", progress=2, stage="Klanginventur")
            manifest = self.manifest(pid)
            source = self.folder(pid) / manifest["asset"]["stored_name"]
            # Inventory is a prior hypothesis, not proof of separated sources.
            try:
                inventory = inspect_mini(source)
                selected = [
                    {"id": item["id"], "label": item["label"], "enabled": True}
                    for item in inventory.get("detections", [])
                    if item.get("suggested", False)
                ]
                with self.lock:
                    updated = self.manifest(pid)
                    updated["inventory"] = inventory
                    if not updated.get("plan"):
                        updated["plan"] = selected
                    self.write_manifest(pid, updated)
                stages.append("Lokale KI-Klanginventur (Hypothesen)")
            except (ValueError, FileNotFoundError) as exc:
                warnings.append("Mini-KI nicht verfügbar: " + str(exc))
            self._fusion_stage(job_id, "Grundquellen separieren", 12, self.run_job)
            stages.append("Demucs Vier-Stem-Separation" if method == "demucs"
                          else "HPSS Harmonik/Percussion (DSP)")
            # Bass extraction reads exactly these separation outputs; not a new
            # analysis of a mysterious input or extra copy of the original.
            bass_mode = "demucs" if method == "demucs" else "hpss"
            neutral_bass = BassRequest(
                project_id=pid, source_mode=bass_mode, wet=0, harmonics=0,
                kick_gain_db=0, bass_gain_db=0, sub_gain_db=0,
            )
            self.jobs[job_id]["bass_source"] = (
                "Demucs: bass + drums" if method == "demucs"
                else "HPSS: harmonic + percussive"
            )
            # Existing run_bass_job reads mode from child job metadata.
            child_id = str(uuid.uuid4())
            with self.lock:
                self.jobs[child_id] = {
                    "id": child_id, "project_id": pid, "kind": "fusion_internal",
                    "mode": bass_mode, "status": "running", "progress": 0, "error": None,
                }
            self.update_job(job_id, progress=43, stage="Kick und Bassline ableiten")
            try:
                self.run_bass_job(child_id, neutral_bass)
                if self.jobs[child_id]["status"] != "done":
                    raise RuntimeError(self.jobs[child_id].get("error") or "Bass fehlgeschlagen")
            finally:
                with self.lock:
                    self.jobs.pop(child_id, None)
            stages.append("Kick/Bassline als kompensierte DSP-Schätzspuren")
            if options.get("scan_noise", True):
                current = self.manifest(pid)
                old = current.get("noise") or {}
                if old.get("scan") and old.get("source_sha256") == current["asset"]["sha256"]:
                    stages.append("Vorhandenen Störstellen-Scan behalten")
                else:
                    self._fusion_stage(job_id, "Störstellen erkennen (ohne Entfernung)", 68,
                                       self.run_noise_job, "scan", [], 0.8)
                    stages.append("NOISE-CORE: Klickverdacht nur markiert")
            current = self.manifest(pid)
            if options.get("include_event_proxies", True) and current.get("plan"):
                self._fusion_stage(job_id, "Planfähigkeiten und Elemente prüfen", 83,
                                   self.run_omnistem_job)
                stages.append("OMNISTEM: nur verfügbare DSP-Proxies / vorhandene Stems")
            final = self.manifest(pid)
            active = self.active_tracks(final)
            lineage = {}
            parents = {
                "kick_core": "Demucs Drums-Stem" if method == "demucs" else "HPSS Percussive-Stem",
                "bassline_core": "Demucs Bass-Stem" if method == "demucs" else "HPSS Harmonic-Stem",
                "noise_difference": "Manuell freigegebene Reparatur des Originalsignals",
                "snare_proxy": "Drums-Stem · Mittenimpuls-Proxy" if method == "demucs" else "Percussive-Stem · Mittenimpuls-Proxy",
                "hat_proxy": "Drums-Stem · Höhenimpuls-Proxy" if method == "demucs" else "Percussive-Stem · Höhenimpuls-Proxy",
            }
            for name, info in active.items():
                lineage[name] = {
                    "label": info.get("label", name),
                    "role": info.get("role", name),
                    "derived_from": parents.get(name) or info.get("derived_from") or (
                        "Originalmix → Demucs KI-Separation" if method == "demucs"
                        else "Originalmix → HPSS-DSP-Separation"
                    ),
                    "type": (
                        "neural" if method == "demucs" and name in ("vocals", "drums", "other", "bass")
                        else "dsp_estimate" if name in ("kick_core", "bassline_core", "snare_proxy", "hat_proxy")
                        else "approved_repair" if name == "noise_difference"
                        else "signal_component"
                    ),
                }
            missing = []
            if method != "demucs":
                missing.append("Vocals sind NICHT isoliert: lokales Demucs-Modell fehlt oder DSP-Modus gewählt.")
            if not (final.get("noise") or {}).get("repair"):
                missing.append("Störspur ist noch nicht erzeugt: erkannte Klicks erst manuell freigeben.")
            fusion = {
                "id": job_id, "name": "Ω FUSION", "completed_at": timestamp(),
                "source_sha256": final["asset"]["sha256"],
                "method": method, "mode_requested": options["mode"],
                "bass_parent": (
                    {"kick": "drums", "bassline": "bass"} if method == "demucs"
                    else {"kick": "percussive", "bassline": "harmonic"}
                ),
                "stages": stages, "stems": lineage, "warnings": warnings + missing,
                "noise_candidates": len(((final.get("noise") or {}).get("scan") or {}).get("events") or []),
                "noise_removal_automatic": False,
                "mixture_consistent": bool((final.get("unified") or {}).get("mix_consistent")),
            }
            with self.lock:
                newest = self.manifest(pid)
                newest["fusion"] = fusion
                self.write_manifest(pid, newest)
            self.update_job(job_id, progress=100, status="done",
                            stage=f"Fertig: {len(lineage)} tatsächlich vorhandene Audiospuren")
        except Exception as exc:
            self.update_job(job_id, status="failed", stage="Angehalten",
                            error=f"{type(exc).__name__}: {exc}")


    def create_unified_job(self, project_id: str) -> dict:
        project_id = check_id(project_id)
        with self.lock:
            manifest = self.manifest(project_id)
            if not manifest.get("asset"):
                raise HTTPException(status_code=409, detail="Import audio before building tracks.")
            if any(j["project_id"]==project_id and j["status"] in ("queued","running")
                   for j in self.jobs.values()):
                raise HTTPException(status_code=409, detail="Project already processing.")
            job={"id":str(uuid.uuid4()),"project_id":project_id,
                 "kind":"unified", "model":"canonical_track_graph",
                 "status":"queued","progress":0,"created_at":timestamp(),"error":None}
            job["ns_root"] = str(self.data_root); self.jobs[job["id"]]=job
            self.executor.submit(self.run_unified_job,job["id"])
            return {k: v for k, v in job.items() if k != "ns_root"}

    def run_unified_job(self, job_id: str):
        pid=self.jobs[job_id]["project_id"]
        try:
            self.update_job(job_id,status="running",progress=10)
            self.refresh_unified(pid)
            self.update_job(job_id,status="done",progress=100)
        except Exception as exc:
            self.update_job(job_id,status="failed",error=f"{type(exc).__name__}: {exc}")

    def create_mixer_job(self, request: MixerRequest) -> dict:
        project_id=check_id(request.project_id)
        with self.lock:
            manifest=self.manifest(project_id)
            tracks = self.active_tracks(manifest)
            if not tracks:
                raise HTTPException(status_code=409,detail="Run separation or build BASS/NOISE tracks first.")
            unknown=set(request.tracks)-set(tracks)
            if unknown:
                raise HTTPException(status_code=422,detail="Unknown real stems: "+", ".join(sorted(unknown)))
            if len(tracks)>128:
                raise HTTPException(status_code=422,detail="More than 128 stems not supported for one render.")
            if (request.tempo!=1 or request.pitch_semitones!=0) and not rubberband_available():
                raise HTTPException(status_code=422,detail="FFmpeg with rubberband is required for independent tempo/pitch.")
            if any(j["project_id"]==project_id and j["status"] in ("queued","running")
                   for j in self.jobs.values()):
                raise HTTPException(status_code=409,detail="Project already processing.")
            job={"id":str(uuid.uuid4()),"project_id":project_id,
                 "model":"arrangement_mixer","kind":"mix","status":"queued",
                 "progress":0,"created_at":timestamp(),"error":None}
            job["ns_root"] = str(self.data_root); self.jobs[job["id"]]=job
            self.executor.submit(self.run_mixer_job,job["id"],request.model_dump())
            return {k: v for k, v in job.items() if k != "ns_root"}

    def run_mixer_job(self, job_id: str, settings:dict):
        job=self.jobs[job_id];project_id=job["project_id"]
        try:
            self.update_job(job_id,status="running",progress=3)
            manifest=self.manifest(project_id);folder=self.folder(project_id)
            graph = manifest.get("unified")
            if graph and graph.get("mix_consistent"):
                sources={name:folder/data["master"] for name,data in graph["stems"].items()}
            else:
                sources={name:folder/"stems"/data["master"] for name,data in manifest["stems"].items()}
            output=folder/"mixer"/(job_id+".wav")
            result=render_mix(sources,settings,output,
                              progress=lambda r:self.update_job(job_id,progress=3+int(r*90)))
            with self.lock:
                newest=self.manifest(project_id)
                newest["mixer"]={"id":job_id,"file":output.name,"settings":settings,
                                 "result":result,"created_at":timestamp()}
                self.write_manifest(project_id,newest)
            self.update_job(job_id,status="done",progress=100)
        except Exception as exc:
            self.update_job(job_id,status="failed",error=f"{type(exc).__name__}: {exc}")

    def model_info(self) -> list[dict]:
        local = Path(self.model_repo).expanduser() if self.model_repo else None
        configured = bool(local and local.is_dir() and
                          any(local.rglob("*.th")))
        runtime_ready = (importlib.util.find_spec("demucs") is not None
                         and importlib.util.find_spec("torch") is not None)
        return [
            {
                "id": "hpss", "name": "HPSS · deterministic DSP",
                "ready": True, "type": "signal_processing",
                "stems": ["harmonic", "percussive", "residual"],
                "limitations": "Not neural, not instrument-/vocal-specific.",
            },
            {
                "id": "demucs", "name": "Demucs HT v4 · neural (local weights)",
                "ready": configured and runtime_ready, "type": "neural_network",
                "checkpoint_configured": configured, "runtime_ready": runtime_ready,
                "stems": ["drums", "bass", "other", "vocals", "residual"],
                "limitations": ("Requires trusted local htdemucs weights/config (official checkpoint 955717e8) and optional "
                                "demucs/torch dependencies. No automatic downloads."),
            },
        ]

    def create_job(self, request: SeparationRequest) -> dict:
        project_id = check_id(request.project_id)
        with self.lock:
            manifest = self.manifest(project_id)
            if not manifest["asset"]:
                raise HTTPException(status_code=409, detail="Import audio before separation.")
            if request.model == "demucs" and not self.model_info()[1]["ready"]:
                raise HTTPException(status_code=409, detail="Demucs not configured; local checkpoints required.")
            if request.model == "demucs" and DEMUCS_MAX_SECONDS > 0:
                duration = float((manifest.get("analysis") or {}).get("duration_seconds") or 0)
                if duration > DEMUCS_MAX_SECONDS:
                    raise HTTPException(status_code=413, detail=(
                        f"Demucs on this CPU instance is limited to {DEMUCS_MAX_SECONDS:g} s audio "
                        f"(file: {duration:.0f} s). Use HPSS or run locally."))
            if request.target_description.strip() and request.model == "hpss":
                raise HTTPException(status_code=422, detail="HPSS cannot follow arbitrary text prompts.")
            # Explicitly reject the unsupported "text conditioned" interpretation for neural.
            if request.target_description.strip() and request.model == "demucs":
                raise HTTPException(status_code=422, detail="Demucs v4 here offers fixed 4 stems, not text prompting.")
            if any(job["project_id"] == project_id and job["status"] in ("queued", "running")
                   for job in self.jobs.values()):
                raise HTTPException(status_code=409, detail="A project job is already running.")
            job = {
                "id": str(uuid.uuid4()), "project_id": project_id, "model": request.model,
                "status": "queued", "progress": 0, "created_at": timestamp(), "error": None,
            }
            job["ns_root"] = str(self.data_root); self.jobs[job["id"]] = job
            self.executor.submit(self.run_job, job["id"])
            return {k: v for k, v in job.items() if k != "ns_root"}

    def create_bass_job(self, request: BassRequest) -> dict:
        project_id = check_id(request.project_id)
        with self.lock:
            manifest = self.manifest(project_id)
            if not manifest["asset"]:
                raise HTTPException(status_code=409, detail="Import audio before BASS-CORE.")
            active = any(j["project_id"] == project_id and j["status"] in ("queued", "running")
                         for j in self.jobs.values())
            if active:
                raise HTTPException(status_code=409, detail="A project job is already running.")
            stems = manifest.get("stems") or {}
            separation = (manifest.get("separation") or {}).get("method")
            valid_hpss = separation == "hpss" and all(k in stems for k in ("harmonic","percussive"))
            valid_neural = separation == "demucs" and all(k in stems for k in ("bass","drums"))
            mode = request.source_mode
            if mode == "auto":
                mode = "demucs" if valid_neural else "hpss" if valid_hpss else "dsp"
            if mode == "hpss" and not valid_hpss:
                raise HTTPException(status_code=409, detail="Run HPSS separation first.")
            if mode == "demucs" and not valid_neural:
                raise HTTPException(status_code=409, detail="Run Demucs bass/drums separation first.")
            job = {
                "id": str(uuid.uuid4()), "project_id": project_id, "model": "basscore",
                "mode": mode, "kind": "bass", "status": "queued", "progress": 0,
                "created_at": timestamp(), "error": None,
            }
            job["ns_root"] = str(self.data_root); self.jobs[job["id"]] = job
            self.executor.submit(self.run_bass_job, job["id"], request)
            return {k: v for k, v in job.items() if k != "ns_root"}

    def run_bass_job(self, job_id: str, request: BassRequest) -> None:
        job = self.jobs[job_id]
        project_id, mode = job["project_id"], job["mode"]
        try:
            self.update_job(job_id, status="running", progress=5)
            manifest = self.manifest(project_id)
            folder = self.folder(project_id)
            refs = {}
            wanted = {"dsp": (), "hpss": ("harmonic","percussive"),
                      "demucs": ("bass","drums")}[mode]
            for name in wanted:
                refs[name] = folder / "stems" / manifest["stems"][name]["master"]
            settings = BassParams(**request.model_dump(exclude={"project_id","source_mode"}))
            out = folder / "basscore" / job_id
            stems, report = process_bass_file(
                folder / manifest["asset"]["stored_name"], out, settings,
                mode=mode, refs=refs,
                progress=lambda ratio:self.update_job(job_id, progress=5 + int(90 * ratio)),
            )
            result = {"id": job_id, "completed_at": timestamp(), "mode": mode,
                      "source_sha256": manifest["asset"]["sha256"],
                      "stems": stems, "report": report}
            with self.lock:
                updated = self.manifest(project_id)
                updated["bass"] = result
                updated["unified"] = None
                self.write_manifest(project_id, updated)
            self.update_job(job_id, progress=94)
            self.refresh_unified(project_id)
            self.update_job(job_id, status="done", progress=100)
        except Exception as exc:
            self.update_job(job_id, status="failed", error=f"{type(exc).__name__}: {exc}")

    def create_noise_job(self, project_id: str, action: str,
                         approved: list[int] | None=None, strength: float=0.8) -> dict:
        project_id=check_id(project_id)
        with self.lock:
            manifest=self.manifest(project_id)
            if not manifest.get("asset"):
                raise HTTPException(status_code=409,detail="Import audio before NOISE-CORE.")
            if any(j["project_id"]==project_id and j["status"] in ("queued","running")
                   for j in self.jobs.values()):
                raise HTTPException(status_code=409,detail="A project job is already running.")
            if action=="repair":
                scan=(manifest.get("noise") or {}).get("scan")
                if not scan:
                    raise HTTPException(status_code=409,detail="Scan for click hypotheses first.")
                allowed={event["sample"] for event in scan["events"]}
                if not set(approved or []).issubset(allowed):
                    raise HTTPException(status_code=422,detail="Repair only confirmed scan events.")
                if not approved:
                    raise HTTPException(status_code=422,detail="Select at least one click candidate.")
            job={
                "id":str(uuid.uuid4()),"project_id":project_id,
                "model":"noisecore","kind":"noise_"+action,
                "status":"queued","progress":0,"created_at":timestamp(),"error":None,
            }
            job["ns_root"] = str(self.data_root); self.jobs[job["id"]]=job
            self.executor.submit(self.run_noise_job,job["id"],action,approved or [],strength)
            return {k: v for k, v in job.items() if k != "ns_root"}

    def run_noise_job(self, job_id:str, action:str, approved:list[int], strength:float)->None:
        project_id=self.jobs[job_id]["project_id"]
        try:
            self.update_job(job_id,status="running",progress=3)
            manifest=self.manifest(project_id)
            folder=self.folder(project_id)
            source=folder/manifest["asset"]["stored_name"]
            callback=lambda ratio:self.update_job(job_id,progress=5+int(90*ratio))
            if action=="scan":
                scan=inspect_noise_file(source,progress=callback)
                with self.lock:
                    updated=self.manifest(project_id)
                    # Old repair, if any, must not be attached to a new scan.
                    updated["noise"]={"scan":scan,"repair":None,
                                      "source_sha256":manifest["asset"]["sha256"],
                                      "scanned_at":timestamp()}
                    self.write_manifest(project_id,updated)
            else:
                root=folder/"noisecore"/job_id
                tracks,report=repair_noise_file(
                    source,root,approved,RepairParams(strength),progress=callback)
                with self.lock:
                    updated=self.manifest(project_id)
                    updated["noise"]["repair"]={
                        "id":job_id,"completed_at":timestamp(),
                        "source_sha256":manifest["asset"]["sha256"],
                        "tracks":tracks,"report":report,
                    }
                    self.write_manifest(project_id,updated)
            self.update_job(job_id,progress=94)
            self.refresh_unified(project_id)
            self.update_job(job_id,status="done",progress=100)
        except Exception as exc:
            self.update_job(job_id,status="failed",error=f"{type(exc).__name__}: {exc}")

    def create_virtualbass_job(self, request: VirtualBassRequest) -> dict:
        pid=check_id(request.project_id)
        with self.lock:
            manifest=self.manifest(pid)
            if not manifest.get("asset"):
                raise HTTPException(status_code=409,detail="Import stereo audio first.")
            if not virtualbass_available():
                raise HTTPException(status_code=409,detail="FFmpeg virtualbass filter not available locally.")
            if manifest["analysis"]["channels"]!=2:
                raise HTTPException(status_code=422,detail="Virtualbass requires two-channel stereo.")
            if any(j["project_id"]==pid and j["status"] in ("queued","running")
                   for j in self.jobs.values()):
                raise HTTPException(status_code=409,detail="Project has a running job.")
            job={"id":str(uuid.uuid4()),"project_id":pid,"model":"ffmpeg_virtualbass",
                 "kind":"virtualbass","status":"queued","progress":0,
                 "created_at":timestamp(),"error":None}
            job["ns_root"] = str(self.data_root); self.jobs[job["id"]]=job
            self.executor.submit(self.run_virtualbass_job,job["id"],request)
            return {k: v for k, v in job.items() if k != "ns_root"}

    def run_virtualbass_job(self, job_id:str, request:VirtualBassRequest) -> None:
        job=self.jobs[job_id]
        pid=job["project_id"]
        try:
            self.update_job(job_id,status="running",progress=10)
            manifest=self.manifest(pid)
            folder=self.folder(pid)
            target=folder/"virtualbass"/job_id/"virtualbass_2_1.wav"
            report=render_virtualbass(
                folder/manifest["asset"]["stored_name"],target,
                cutoff_hz=request.cutoff_hz,strength=request.strength
            )
            self.update_job(job_id,progress=95)
            with self.lock:
                updated=self.manifest(pid)
                updated["virtualbass"]={
                    "id":job_id,"source_sha256":manifest["asset"]["sha256"],
                    "completed_at":timestamp(),"report":report,
                }
                self.write_manifest(pid,updated)
            self.update_job(job_id,status="done",progress=100)
        except Exception as exc:
            self.update_job(job_id,status="failed",error=f"{type(exc).__name__}: {exc}")

    def update_job(self, job_id: str, **kwargs) -> None:
        with self.lock:
            self.jobs[job_id].update(kwargs)

    def run_job(self, job_id: str) -> None:
        job = self.jobs[job_id]
        project_id, model = job["project_id"], job["model"]
        try:
            self.update_job(job_id, status="running", progress=8)
            manifest = self.manifest(project_id)
            folder = self.folder(project_id)
            input_path = folder / manifest["asset"]["stored_name"]
            self.update_job(job_id, progress=20)
            if model == "hpss":
                # Bounded-memory streaming path for long recordings.
                stem_info, report = hpss_separate_file(
                    input_path, folder / "stems",
                    progress=lambda fraction: self.update_job(
                        job_id, progress=20 + int(70 * fraction)
                    ),
                )
            else:
                # Optional neural adapter still depends on available RAM/VRAM.
                original = load_audio(input_path)
                stems = self.run_demucs(original)
                self.update_job(job_id, progress=75)
                report = mixture_report(original.samples, stems)
                stem_info = save_stems(folder / "stems", original, stems)
            self.update_job(job_id, progress=95)
            with self.lock:
                updated = self.manifest(project_id)
                updated["stems"] = stem_info
                updated["separation"] = {
                    "method": model, "completed_at": timestamp(),
                    "architecture": "median_filter_STFT_chunked_v2" if model == "hpss" else "htdemucs_v4",
                    "ai": model == "demucs", "source_sha256": manifest["asset"]["sha256"],
                    "master_format": "WAV 32-bit float",
                    "preview_format": "WAV PCM 16-bit (separate listening copies)",
                }
                updated["report"] = report
                # Bass estimates tied to a prior separation must not survive re-separation.
                if updated.get("bass") and updated["bass"].get("mode") in ("hpss", "demucs"):
                    updated["bass"] = None
                updated["unified"] = None
                updated["omnistem"] = None
                updated["omnistem_base"] = None
                updated["mixer"] = None
                self.write_manifest(project_id, updated)
            self.refresh_unified(project_id)
            self.update_job(job_id, status="done", progress=100)
        except Exception as exc:
            self.update_job(job_id, status="failed", error=f"{type(exc).__name__}: {exc}")

    def run_demucs(self, original):
        """Only runs with explicitly supplied trusted local checkpoints.

        Demucs is optional; checkpoint loading may deserialize pickle. Don't point
        this at untrusted downloaded weights. Never triggers a network fetch.
        """
        if not self.model_repo:
            raise AudioError("Demucs local repository not configured.")
        from demucs.api import Separator  # optional dependency
        import torch
        separator = Separator(
            model="htdemucs", repo=Path(self.model_repo).expanduser(),
            device="cuda" if torch.cuda.is_available() else "cpu",
            shifts=0, overlap=0.25, split=True,
        )
        signal = torch.from_numpy(original.samples.T.astype(np.float32))
        with torch.inference_mode():
            _mix, sources = separator.separate_tensor(signal, sr=original.samplerate)
        stems = {}
        for name, value in sources.items():
            array = value.detach().cpu().numpy().T
            stems[name] = align_and_resample(
                array, separator.samplerate, original.samplerate, len(original.samples)
            )
        accum = np.zeros_like(original.samples)
        for value in stems.values():
            accum += value
        stems["residual"] = original.samples - accum
        return stems


def writable_dir(preferred: Path, fallback: Path) -> Path:
    """Use the persistent mount (e.g. HF bucket at /data) if writable, else fallback."""
    try:
        preferred.mkdir(parents=True, exist_ok=True)
        probe = preferred / f".probe-{uuid.uuid4().hex}"
        probe.write_text("ok"); probe.unlink()
        return preferred
    except OSError:
        fallback.mkdir(parents=True, exist_ok=True)
        return fallback


def create_app(data_root: Path | None = None) -> FastAPI:
    default = Path(os.environ.get("ABSOLUTION_DATA_DIR", "./omega_data"))
    if data_root is None:
        data_root = writable_dir(default, Path(os.environ.get("ABSOLUTION_FALLBACK_DIR", "/tmp/omega_data")))
    service = Service(data_root)
    sessions = access.Sessions(service.base_root)
    sessions.start_cleanup_thread()
    app = FastAPI(title="Tune-Whisperer", version="1.1.0",
                  description="Local-first audio forensic vertical slice")
    app.state.service = service
    static_dir = Path(__file__).parent / "static"
    app.mount("/static", StaticFiles(directory=static_dir), name="static")

    def _cookie_secure(scope) -> bool:
        headers = dict(scope.get("headers") or [])
        proto = headers.get(b"x-forwarded-proto", b"").decode() or scope.get("scheme", "http")
        return proto == "https"

    class SessionMiddleware:
        def __init__(self, inner):
            self.inner = inner
        async def __call__(self, scope, receive, send):
            if scope["type"] != "http":
                return await self.inner(scope, receive, send)
            raw = dict(scope.get("headers") or []).get(b"cookie", b"").decode("latin-1")
            jar = SimpleCookie(); 
            try: jar.load(raw)
            except Exception: pass
            session = sessions.verify(jar[access.COOKIE].value if access.COOKIE in jar else None)
            fresh = session is None
            if fresh:
                session = sessions.new_anonymous()
            ns, root = sessions.namespace(session)
            scope.setdefault("state", {})
            scope["state"]["omega_session"] = session
            t1 = access.current_root.set(root)
            t2 = access.current_identity.set({"ns": ns, "session": session})
            async def send_wrapper(message):
                if message["type"] == "http.response.start" and fresh:
                    hdrs = list(message.get("headers") or [])
                    hdrs.append((b"set-cookie", _session_cookie(sessions.sign(session), _cookie_secure(scope)).encode()))
                    message["headers"] = hdrs
                await send(message)
            try:
                await self.inner(scope, receive, send_wrapper)
            finally:
                access.current_root.reset(t1); access.current_identity.reset(t2)

    def _session_cookie(value: str, secure: bool) -> str:
        attrs = f"{access.COOKIE}={value}; Path=/; Max-Age={access.SESSION_MAX_AGE}; HttpOnly"
        return attrs + ("; Secure; SameSite=None" if secure else "; SameSite=Lax")

    app.add_middleware(SessionMiddleware)

    @app.get("/api/v1/me")
    def me():
        ident = access.current_identity.get() or {}
        sess = ident.get("session") or {}
        return {"owner": access.Sessions.is_owner(sess), "user": sess.get("user"),
                "signed_in": sess.get("kind") == "hf", "namespace": "owner" if ident.get("ns") == "owner" else "guest",
                "guest_ttl_hours": access.GUEST_TTL_SECONDS / 3600,
                "oauth_available": access.oauth_config() is not None}

    @app.get("/login")
    def login(request: Request):
        cfg = access.oauth_config()
        if not cfg:
            raise HTTPException(status_code=503, detail="HF OAuth not configured for this Space.")
        state = _secrets.token_urlsafe(24)
        resp = RedirectResponse(access.authorize_url(cfg, access.redirect_uri(cfg, str(request.base_url)), state), status_code=302)
        resp.set_cookie("omega_oauth_state", state, max_age=600, httponly=True,
                        secure=_cookie_secure(request.scope), samesite="none" if _cookie_secure(request.scope) else "lax")
        return resp

    @app.get("/login/callback")
    def login_callback(request: Request, code: str = "", state: str = ""):
        cfg = access.oauth_config()
        if not cfg:
            raise HTTPException(status_code=503, detail="HF OAuth not configured.")
        expected = request.cookies.get("omega_oauth_state")
        if not code or not state or not expected or not _secrets.compare_digest(state, expected):
            raise HTTPException(status_code=400, detail="Invalid OAuth state.")
        try:
            redirect = access.redirect_uri(cfg, str(request.base_url))
            username = access.fetch_username(cfg, access.exchange_code(cfg, code, redirect))
        except Exception as exc:
            raise HTTPException(status_code=502, detail=f"OAuth failed: {type(exc).__name__}") from exc
        session = sessions.for_user(username, request.scope.get("state", {}).get("omega_session"))
        resp = RedirectResponse("/", status_code=302)
        resp.headers.append("set-cookie", _session_cookie(sessions.sign(session), _cookie_secure(request.scope)))
        resp.delete_cookie("omega_oauth_state")
        return resp

    @app.get("/logout")
    def logout(request: Request):
        resp = RedirectResponse("/", status_code=302)
        anon = sessions.new_anonymous()
        resp.headers.append("set-cookie", _session_cookie(sessions.sign(anon), _cookie_secure(request.scope)))
        return resp

    @app.get("/")
    def home():
        return FileResponse(static_dir / "index.html", media_type="text/html")

    @app.get("/api/v1/demo")
    def demo_audio():
        """Serve a bundled, entirely synthetic WAV without contacting the network."""
        sample = static_dir / "demo.wav"
        if not sample.is_file():
            raise HTTPException(status_code=404, detail="Demo audio not installed.")
        return FileResponse(sample, media_type="audio/wav", filename="tune-whisperer_demo.wav")

    @app.get("/api/v1/health")
    def health():
        return {"status": "ok", "app": "Tune-Whisperer", "version": "1.1.0", "fusion_pipeline": True, "omnistem_plan_runner": True, "unified_stem_graph": True, "arrangement_studio": True, "mini_tag_model_ready": MODEL_FILE.is_file(), "rubberband_ready": rubberband_available(), "basscore": True, "noisecore": True, "virtualbass_ready": virtualbass_available(), "job_backend": "process-local", "persistent_storage": str(service.base_root).startswith("/data"), "guest_isolation": True, "oauth_configured": access.oauth_config() is not None, "demucs_max_seconds": DEMUCS_MAX_SECONDS or None,
                 "network_downloads": False, "auth": "hf_oauth_owner_only_library",
                "max_upload_bytes": MAX_UPLOAD_BYTES,
                "duration_limit_seconds": None, "streaming_hpss": True}

    @app.get("/api/v1/models")
    def models():
        return {"models": service.model_info()}

    @app.post("/api/v1/projects", status_code=201)
    def create_project(data: ProjectCreate):
        return service.create_project(data.name)

    @app.get("/api/v1/projects")
    def list_projects():
        """Local library: one explicitly labelled source asset per project."""
        items = []
        with service.lock:
            for folder in service.data_root.iterdir():
                if not folder.is_dir() or not (folder / "manifest.json").is_file():
                    continue
                try:
                    m = json.loads((folder / "manifest.json").read_text(encoding="utf-8"))
                    if folder.name != check_id(str(m.get("id", ""))):
                        continue
                    items.append({
                        "id": m["id"], "name": m.get("name", "Unbenannt"),
                        "created_at": m.get("created_at"),
                        "asset": m.get("asset"),
                        "analysis": m.get("analysis"),
                        "stem_count": len(service.active_tracks(m)),
                    })
                except (ValueError, KeyError, TypeError, json.JSONDecodeError, HTTPException):
                    continue
        items.sort(key=lambda x: x.get("created_at") or "", reverse=True)
        return {"projects": items}

    @app.patch("/api/v1/projects/{project_id}")
    def rename_project(project_id: str, req: ProjectRename):
        with service.lock:
            m = service.manifest(project_id)
            m["name"] = req.name.strip()
            if not m["name"]:
                raise HTTPException(status_code=422, detail="Projektname darf nicht leer sein.")
            service.write_manifest(project_id, m)
            return {"id": project_id, "name": m["name"]}

    @app.get("/api/v1/projects/{project_id}")
    def get_project(project_id: str):
        return service.manifest(project_id)

    @app.post("/api/v1/assets", status_code=201)
    async def upload_asset(project_id: str = Form(...), file: UploadFile = File(...)):
        project_id = check_id(project_id)
        original_name = Path(file.filename or "").name[:120]
        suffix = Path(original_name).suffix.lower()
        if suffix not in ALLOWED_EXTENSIONS:
            raise HTTPException(status_code=415, detail="Unsupported audio file extension.")
        with service.lock:
            manifest = service.manifest(project_id)
            if manifest["asset"] is not None:
                raise HTTPException(status_code=409, detail="One imported file per project in v0.1.")
            if any(j["project_id"] == project_id and j["status"] in ("queued", "running")
                   for j in service.jobs.values()):
                raise HTTPException(status_code=409, detail="Project is busy.")
            stored_name = "source" + suffix
            path = service.folder(project_id) / stored_name
            written = 0
            try:
                with path.open("xb") as destination:
                    while True:
                        part = await file.read(1024 * 1024)
                        if not part:
                            break
                        written += len(part)
                        if written > MAX_UPLOAD_BYTES:
                            raise HTTPException(status_code=413, detail=f"Max upload size is {MAX_UPLOAD_MIB} MiB.")
                        destination.write(part)
                stats = analyze_file(path, service.folder(project_id) / "original_preview.wav")
                asset = {
                    "id": str(uuid.uuid4()), "filename": original_name,
                    "stored_name": stored_name, "sha256": sha256_file(path),
                    "size_bytes": written,
                }
                manifest["asset"], manifest["analysis"] = asset, stats
                service.write_manifest(project_id, manifest)
                return {"asset": asset, "analysis": stats, "project_id": project_id}
            except (AudioError, ValueError) as exc:
                path.unlink(missing_ok=True)
                (service.folder(project_id) / "original_preview.wav").unlink(missing_ok=True)
                raise HTTPException(status_code=422, detail=str(exc)) from exc
            except Exception:
                path.unlink(missing_ok=True)
                raise
            finally:
                await file.close()

    @app.post("/api/v1/jobs/fusion", status_code=202)
    def start_fusion(request: FusionRequest):
        return service.create_fusion_job(request)

    @app.post("/api/v1/jobs/separate", status_code=202)
    def start_separation(request: SeparationRequest):
        return service.create_job(request)

    @app.get("/api/v1/jobs/{job_id}")
    def get_job(job_id: str):
        try:
            job = dict(service.jobs[str(uuid.UUID(job_id))])
        except (ValueError, KeyError) as exc:
            raise HTTPException(status_code=404, detail="Job not found.") from exc
        if job.get("ns_root") != str(service.data_root):
            raise HTTPException(status_code=404, detail="Job not found.")
        job.pop("ns_root", None)
        return job

    @app.post("/api/v1/jobs/bass", status_code=202)
    def bass_job(request: BassRequest):
        return service.create_bass_job(request)

    @app.get("/api/v1/projects/{project_id}/bass")
    def bass_results(project_id: str):
        result = service.manifest(project_id).get("bass")
        return {"bass": result}

    @app.get("/api/v1/projects/{project_id}/bass/audio/{track}")
    def bass_audio(project_id: str, track: str, preview: bool = False):
        manifest = service.manifest(project_id)
        result = manifest.get("bass")
        if not result or track not in TRACKS or track not in result["stems"]:
            raise HTTPException(status_code=404, detail="Bass track not found.")
        info = result["stems"][track]
        key = "preview" if preview else "master"
        path = service.folder(project_id) / "basscore" / result["id"] / info[key]
        if not path.is_file():
            raise HTTPException(status_code=404, detail="Bass track not found.")
        return FileResponse(path, media_type="audio/wav", filename=path.name)

    @app.get("/api/v1/projects/{project_id}/bass/export")
    def bass_export(project_id: str):
        manifest = service.manifest(project_id)
        result = manifest.get("bass")
        if not result:
            raise HTTPException(status_code=409, detail="Run BASS-CORE before exporting.")
        folder = service.folder(project_id)
        root = folder / "basscore" / result["id"]
        target = root / "BASS_CORE_EXPORT.zip"
        tmp = root / f".{uuid.uuid4().hex}.zip"
        with zipfile.ZipFile(tmp, "w", compression=zipfile.ZIP_STORED) as archive:
            archive.writestr("bass_report.json", json.dumps(result,indent=2,ensure_ascii=False))
            for name, entry in result["stems"].items():
                archive.write(root / entry["master"], f"stems/{entry['master']}")
        tmp.replace(target)
        return FileResponse(target,media_type="application/zip",filename="ABSOLUTION_BASS_CORE.zip")

    @app.post("/api/v1/jobs/noise/scan",status_code=202)
    def noise_scan(request:NoiseScanRequest):
        return service.create_noise_job(request.project_id,"scan")

    @app.post("/api/v1/jobs/noise/repair",status_code=202)
    def noise_repair(request:NoiseRepairRequest):
        return service.create_noise_job(request.project_id,"repair",
                                         request.approved_samples,request.strength)

    @app.get("/api/v1/projects/{project_id}/noise")
    def noise_results(project_id:str):
        return {"noise":service.manifest(project_id).get("noise")}

    @app.get("/api/v1/projects/{project_id}/noise/audio/{track}")
    def noise_audio(project_id:str, track:str, preview:bool=False):
        result=(service.manifest(project_id).get("noise") or {}).get("repair")
        if not result or track not in ("clean","difference"):
            raise HTTPException(status_code=404,detail="NOISE-CORE track not found.")
        item=result["tracks"][track]
        path=service.folder(project_id)/"noisecore"/result["id"]/item[
            "preview" if preview else "master"]
        if not path.is_file():
            raise HTTPException(status_code=404,detail="NOISE-CORE file not found.")
        return FileResponse(path,media_type="audio/wav",filename=path.name)

    @app.get("/api/v1/projects/{project_id}/noise/export")
    def noise_export(project_id:str):
        result=(service.manifest(project_id).get("noise") or {}).get("repair")
        if not result:
            raise HTTPException(status_code=409,detail="Approve and render repairs first.")
        folder=service.folder(project_id)/"noisecore"/result["id"]
        tmp=folder/f".{uuid.uuid4().hex}.zip"
        target=folder/"NOISE_CORE_EXPORT.zip"
        with zipfile.ZipFile(tmp,"w",compression=zipfile.ZIP_STORED) as archive:
            archive.writestr("report.json",json.dumps(result,indent=2,ensure_ascii=False))
            for name,entry in result["tracks"].items():
                archive.write(folder/entry["master"],f"tracks/{entry['master']}")
        tmp.replace(target)
        return FileResponse(target,media_type="application/zip",
                            filename="ABSOLUTION_NOISE_CORE.zip")

    @app.post("/api/v1/jobs/bass/virtual",status_code=202)
    def create_virtual(request:VirtualBassRequest):
        return service.create_virtualbass_job(request)

    @app.get("/api/v1/projects/{project_id}/virtualbass")
    def virtual_result(project_id:str):
        return {"virtualbass":service.manifest(project_id).get("virtualbass")}

    @app.get("/api/v1/projects/{project_id}/virtualbass/audio")
    def virtual_audio(project_id:str):
        result=service.manifest(project_id).get("virtualbass")
        if not result:
            raise HTTPException(status_code=404,detail="No Virtual Bass result.")
        path=service.folder(project_id)/"virtualbass"/result["id"]/"virtualbass_2_1.wav"
        if not path.is_file():
            raise HTTPException(status_code=404,detail="Virtual Bass file not found.")
        return FileResponse(path,media_type="audio/wav",filename="ABSOLUTION_VIRTUAL_BASS_2_1.wav")

    @app.post("/api/v1/projects/{project_id}/inventory")
    def run_inventory(project_id:str):
        project_id=check_id(project_id)
        with service.lock:
            manifest=service.manifest(project_id)
            if not manifest.get("asset"):
                raise HTTPException(status_code=409,detail="Audio import required.")
            path=service.folder(project_id)/manifest["asset"]["stored_name"]
            try:
                inventory=inspect_mini(path)
            except (ValueError,FileNotFoundError) as exc:
                raise HTTPException(status_code=422,detail=str(exc)) from exc
            # Editable suggestions are hypotheses, NOT separated audio.
            suggested=[{"id":d["id"],"label":d["label"],"enabled":True}
                       for d in inventory["detections"] if d["suggested"]]
            manifest["inventory"]=inventory
            if not manifest.get("plan"):manifest["plan"]=suggested
            service.write_manifest(project_id,manifest)
            return {"inventory":inventory,"plan":manifest["plan"],
                    "actual_stems":list(manifest.get("stems",{}))}

    @app.put("/api/v1/projects/{project_id}/plan")
    def update_plan(project_id:str,req:PlanRequest):
        ids=[x.id for x in req.tracks]
        if len(ids)!=len(set(ids)):
            raise HTTPException(status_code=422,detail="Duplicate planned IDs.")
        with service.lock:
            manifest=service.manifest(project_id)
            if not manifest.get("asset"):
                raise HTTPException(status_code=409,detail="Import audio first.")
            manifest["plan"]=[x.model_dump() for x in req.tracks]
            service.write_manifest(project_id,manifest)
            return {"plan":manifest["plan"],"status":"hypotheses_only",
                    "notice":"A saved plan does not separate or generate new stems."}

    @app.get("/api/v1/projects/{project_id}/omnistem/plan")
    def preview_omnistem_plan(project_id: str):
        manifest = service.manifest(project_id)
        if not manifest.get("asset"):
            raise HTTPException(status_code=409, detail="Audio import required.")
        active = service.active_tracks(manifest)
        return compile_plan(manifest.get("plan") or [], active)

    @app.post("/api/v1/projects/{project_id}/omnistem/run", status_code=202)
    def run_omnistem_plan(project_id: str):
        return service.create_omnistem_job(project_id)

    @app.get("/api/v1/projects/{project_id}/omnistem/report")
    def get_omnistem_report(project_id: str):
        manifest = service.manifest(project_id)
        return {"omnistem": manifest.get("omnistem")}

    @app.get("/api/v1/projects/{project_id}/wave/{stem}")
    def get_wave(project_id:str,stem:str,points:int=360):
        manifest=service.manifest(project_id);folder=service.folder(project_id)
        if stem=="original":
            if not manifest.get("asset"):raise HTTPException(status_code=404,detail="No audio")
            path=folder/manifest["asset"]["stored_name"]
        else:
            graph=manifest.get("unified")
            active=service.active_tracks(manifest)
            if stem not in active:
                raise HTTPException(status_code=404,detail="Unknown stem")
            path=folder/active[stem]["master"] if (graph and graph.get("mix_consistent")) else folder/"stems"/active[stem]["master"]
        if points<64 or points>1000:
            raise HTTPException(status_code=422,detail="points must be 64..1000")
        try:return sampled_peaks(path,points)
        except (ValueError,RuntimeError) as exc:
            raise HTTPException(status_code=422,detail=str(exc)) from exc

    @app.post("/api/v1/jobs/unify", status_code=202)
    def start_unified(project_id: str):
        return service.create_unified_job(project_id)

    @app.get("/api/v1/projects/{project_id}/unified")
    def get_unified(project_id: str):
        m = service.manifest(project_id)
        return {"unified": m.get("unified"), "active_stems": service.active_tracks(m)}

    @app.post("/api/v1/jobs/mix",status_code=202)
    def start_mix(request:MixerRequest):
        return service.create_mixer_job(request)

    @app.get("/api/v1/projects/{project_id}/mixer")
    def mixer_status(project_id:str):
        return {"mixer":service.manifest(project_id).get("mixer")}

    @app.get("/api/v1/projects/{project_id}/mixer/audio")
    def mixer_audio(project_id:str):
        manifest=service.manifest(project_id)
        mixer=manifest.get("mixer")
        if not mixer:raise HTTPException(status_code=404,detail="Render a mix first.")
        path=service.folder(project_id)/"mixer"/mixer["file"]
        if not path.is_file():raise HTTPException(status_code=404,detail="Rendered mix missing")
        return FileResponse(path,media_type="audio/wav",filename="ABSOLUTION_ARRANGEMENT_MIX.wav")

    @app.get("/api/v1/projects/{project_id}/stems")
    def get_stems(project_id: str):
        manifest = service.manifest(project_id)
        return {"stems": service.active_tracks(manifest), "report": manifest["report"],
                "separation": manifest["separation"], "unified": manifest.get("unified")}

    @app.get("/api/v1/projects/{project_id}/audio/{stem}")
    def get_audio(project_id: str, stem: str, preview: bool = False):
        folder = service.folder(project_id)
        manifest = service.manifest(project_id)
        if stem == "original":
            if not manifest["asset"]:
                raise HTTPException(status_code=404, detail="Audio not found.")
            path = folder / ("original_preview.wav" if preview else manifest["asset"]["stored_name"])
        else:
            active = service.active_tracks(manifest)
            if stem not in active:
                raise HTTPException(status_code=404, detail="Stem not found.")
            key = "preview" if preview else "master"
            path = folder / active[stem][key] if (manifest.get("unified") or {}).get("mix_consistent") else folder / "stems" / active[stem][key]
        if not path.is_file():
            raise HTTPException(status_code=404, detail="Audio not found.")
        return FileResponse(path, media_type="audio/wav" if preview or path.suffix == ".wav" else "application/octet-stream",
                            filename=path.name if not preview else None)

    @app.get("/api/v1/projects/{project_id}/export")
    def export_stems(project_id: str):
        folder = service.folder(project_id)
        manifest = service.manifest(project_id)
        active = service.active_tracks(manifest)
        if not active:
            raise HTTPException(status_code=409, detail="Separate audio or build BASS/NOISE lanes before exporting.")
        target = folder / "absolution_export.zip"
        # Build only from controlled server-side names. Atomic replace.
        tmp = folder / f".{uuid.uuid4().hex}.zip"
        with zipfile.ZipFile(tmp, "w", compression=zipfile.ZIP_STORED) as archive:
            archive.write(folder / "manifest.json", "manifest.json")
            original_path = folder / manifest["asset"]["stored_name"]
            archive.write(original_path, "source" + original_path.suffix)
            for name,item in active.items():
                path = folder/item["master"] if (manifest.get("unified") or {}).get("mix_consistent") else folder/"stems"/item["master"]
                archive.write(path, "stems/" + name + ".wav")
        tmp.replace(target)
        return FileResponse(target, media_type="application/zip",
                            filename=f"tune-whisperer_{project_id[:8]}.zip")

    return app


app = create_app()