File size: 144,553 Bytes
f7e7379
d043b52
 
 
 
 
 
6ae3c5a
 
d043b52
6ae3c5a
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
 
 
 
 
d043b52
 
62d612f
d043b52
 
f7e7379
 
 
d043b52
 
 
 
 
6ae3c5a
d043b52
 
 
f7e7379
d043b52
f7e7379
 
d043b52
f7e7379
 
 
 
 
 
e54477c
f7e7379
 
 
 
 
 
 
d043b52
 
 
 
 
6ae3c5a
 
d043b52
a40a311
6ae3c5a
d043b52
f7e7379
d043b52
62d612f
d043b52
6ae3c5a
 
 
fee4645
d043b52
 
 
f7e7379
d043b52
 
6ae3c5a
d043b52
 
 
 
 
6ae3c5a
a40a311
6ae3c5a
62d612f
a40a311
f7e7379
 
 
d043b52
f7e7379
 
 
d043b52
f7e7379
d043b52
 
 
a40a311
 
f7e7379
d043b52
 
 
f7e7379
 
d043b52
 
 
f7e7379
d043b52
f7e7379
 
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
6ae3c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d043b52
f7e7379
6ae3c5a
 
 
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
62d612f
f7e7379
d043b52
f7e7379
 
 
 
 
 
 
 
 
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
 
8c042d4
d043b52
8c042d4
f7e7379
8c042d4
d043b52
 
8c042d4
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
 
 
f7e7379
e54477c
d043b52
e54477c
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e54477c
f7e7379
d043b52
f7e7379
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
 
 
d043b52
f7e7379
 
 
d043b52
 
 
f7e7379
 
d043b52
 
f7e7379
d043b52
 
 
 
f7e7379
d043b52
 
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
d043b52
 
 
 
 
f7e7379
d043b52
 
 
 
 
 
f7e7379
 
d043b52
 
f7e7379
d043b52
 
 
 
f7e7379
d043b52
 
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
 
 
d043b52
f7e7379
 
 
d043b52
 
 
f7e7379
 
d043b52
 
f7e7379
 
d043b52
 
 
f7e7379
 
d043b52
 
 
f7e7379
 
d043b52
 
f7e7379
d043b52
6ae3c5a
 
 
 
 
 
 
 
 
d043b52
 
 
 
 
 
 
f7e7379
 
d043b52
 
 
 
 
 
 
 
 
 
 
f7e7379
 
6ae3c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d043b52
 
 
 
 
f7e7379
6ae3c5a
 
 
 
 
d043b52
 
 
 
 
 
 
 
 
 
 
f7e7379
 
6ae3c5a
 
 
 
 
 
d043b52
 
 
 
 
 
 
 
 
6ae3c5a
 
d043b52
 
 
 
6ae3c5a
 
 
 
f7e7379
d043b52
 
 
f7e7379
d043b52
 
 
 
 
f7e7379
6ae3c5a
 
 
 
 
 
 
 
 
 
 
d043b52
 
 
f7e7379
d043b52
 
 
 
 
f7e7379
d043b52
 
 
f7e7379
d043b52
 
 
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
 
6ae3c5a
d043b52
 
 
6ae3c5a
 
d043b52
 
 
 
6ae3c5a
1c8a58d
d043b52
 
 
 
 
 
 
 
6ae3c5a
d043b52
 
 
 
 
 
6ae3c5a
 
 
 
 
d043b52
6ae3c5a
 
d043b52
 
6ae3c5a
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae3c5a
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae3c5a
 
 
 
d043b52
6ae3c5a
 
 
d043b52
 
6ae3c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d043b52
6ae3c5a
d043b52
6ae3c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d043b52
 
 
 
 
 
 
 
 
 
6ae3c5a
 
d043b52
 
 
 
 
 
 
 
 
6ae3c5a
 
 
 
 
 
 
 
 
d043b52
6ae3c5a
 
 
 
 
 
f7e7379
d043b52
f7e7379
6ae3c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
62d612f
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62d612f
 
d043b52
 
 
 
 
 
 
 
 
6ae3c5a
d043b52
 
 
6ae3c5a
 
 
 
 
 
 
 
 
 
d043b52
 
 
 
 
 
 
 
62d612f
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62d612f
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62d612f
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62d612f
 
d043b52
 
 
62d612f
 
d043b52
 
 
62d612f
f7e7379
ac68de5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d043b52
 
ac68de5
6ae3c5a
d043b52
6ae3c5a
ac68de5
62d612f
d043b52
62d612f
f7e7379
ac68de5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae3c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
d043b52
 
6ae3c5a
 
f7e7379
 
 
d043b52
f7e7379
 
 
d043b52
6ae3c5a
 
 
 
 
 
f7e7379
6ae3c5a
d043b52
6ae3c5a
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae3c5a
d043b52
 
 
 
 
 
 
 
 
 
6ae3c5a
 
 
 
 
 
 
 
 
 
d043b52
6ae3c5a
f7e7379
d043b52
 
6ae3c5a
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
d043b52
 
 
 
f7e7379
 
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
 
 
d043b52
 
 
 
 
 
f7e7379
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae3c5a
d043b52
 
 
6ae3c5a
 
 
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
 
 
d043b52
f7e7379
 
 
 
 
 
d043b52
f7e7379
 
d043b52
f7e7379
d043b52
 
f7e7379
d043b52
f7e7379
 
d043b52
 
8c042d4
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a63770b
d043b52
a63770b
 
f7e7379
a63770b
d043b52
 
 
 
 
 
 
 
 
f7e7379
 
d043b52
 
 
 
f7e7379
d043b52
f7e7379
d043b52
 
 
 
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fee4645
 
 
 
 
 
 
d043b52
 
 
 
 
 
f7e7379
 
d043b52
 
f7e7379
d043b52
f7e7379
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
 
d043b52
 
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
 
 
fee4645
d043b52
 
 
 
 
 
fee4645
 
d043b52
 
fee4645
 
 
 
d043b52
 
 
 
 
fee4645
d043b52
f7e7379
 
fee4645
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d043b52
 
 
 
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fee4645
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
fee4645
 
 
d043b52
 
 
 
 
 
 
fee4645
 
 
 
 
d043b52
 
 
fee4645
d043b52
 
 
f7e7379
 
d043b52
 
 
 
 
 
 
 
 
fee4645
d043b52
 
 
 
fee4645
 
d043b52
 
f7e7379
 
d043b52
 
 
 
 
f7e7379
 
a40a311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d043b52
a40a311
 
 
 
 
 
 
d043b52
 
 
 
a40a311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d043b52
 
 
 
 
 
 
 
 
 
 
a40a311
 
 
 
 
 
d043b52
 
a40a311
 
 
 
 
 
 
 
 
 
 
 
 
 
d043b52
 
 
 
 
a40a311
d043b52
 
 
a40a311
d043b52
 
 
 
 
 
 
f7e7379
 
d043b52
 
 
 
 
 
 
 
 
f7e7379
 
d043b52
 
 
f7e7379
d043b52
 
f7e7379
 
d043b52
 
 
 
 
 
 
f7e7379
 
 
 
d043b52
f7e7379
 
 
 
 
d043b52
f7e7379
d043b52
 
f7e7379
 
 
 
d043b52
 
f7e7379
 
 
 
 
 
 
 
 
d043b52
 
 
 
 
 
 
 
f7e7379
d043b52
f7e7379
 
 
d043b52
 
f7e7379
d043b52
 
 
 
f7e7379
d043b52
 
 
f7e7379
d043b52
 
 
 
f7e7379
d043b52
 
f7e7379
 
d043b52
 
 
 
 
 
 
 
 
 
 
f7e7379
d043b52
 
 
f7e7379
 
d043b52
 
 
f7e7379
d043b52
 
 
 
 
 
 
 
ac68de5
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
6ae3c5a
 
d043b52
 
 
 
 
f7e7379
d043b52
 
 
 
 
 
 
 
 
f7e7379
d043b52
 
 
 
 
 
f7e7379
 
 
6ae3c5a
 
 
f7e7379
6ae3c5a
 
 
 
f7e7379
 
6ae3c5a
 
 
 
d043b52
 
6ae3c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
 
d043b52
 
 
 
f7e7379
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae3c5a
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac68de5
d043b52
 
 
 
 
 
 
 
 
 
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae3c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d043b52
f7e7379
d043b52
 
 
 
6ae3c5a
d043b52
 
 
6ae3c5a
 
 
d043b52
 
6ae3c5a
 
 
 
d043b52
 
 
 
 
 
 
 
6ae3c5a
d043b52
 
 
 
 
6ae3c5a
 
 
 
 
 
 
d043b52
 
 
 
6ae3c5a
d043b52
 
 
6ae3c5a
 
 
d043b52
6ae3c5a
 
d043b52
 
 
 
 
 
 
 
6ae3c5a
 
d043b52
 
 
 
 
f7e7379
a40a311
 
6ae3c5a
 
 
 
d043b52
 
6ae3c5a
 
 
d043b52
6ae3c5a
 
 
 
 
 
 
d043b52
 
 
6ae3c5a
d043b52
6ae3c5a
d043b52
6ae3c5a
d043b52
 
 
 
 
a40a311
d043b52
 
 
 
 
 
 
 
 
6ae3c5a
a40a311
6ae3c5a
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae3c5a
 
 
 
 
 
 
d043b52
 
 
 
6ae3c5a
 
 
 
 
d043b52
 
 
 
 
 
 
 
 
f7e7379
 
 
 
 
 
 
6ae3c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac68de5
 
 
 
 
f7e7379
d043b52
 
ac68de5
d043b52
 
f7e7379
6ae3c5a
 
 
 
d043b52
6ae3c5a
ac68de5
d043b52
6ae3c5a
d043b52
ac68de5
d043b52
 
6ae3c5a
d043b52
 
 
 
 
 
 
6ae3c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d043b52
 
 
 
 
 
 
6ae3c5a
 
d043b52
6ae3c5a
 
d043b52
 
 
6ae3c5a
d043b52
6ae3c5a
d043b52
6ae3c5a
 
 
 
 
 
 
 
 
d043b52
f7e7379
6ae3c5a
f7e7379
6ae3c5a
 
 
 
 
f7e7379
6ae3c5a
 
 
 
 
 
d043b52
 
f7e7379
 
 
 
 
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
d043b52
f7e7379
d043b52
 
 
 
 
f7e7379
d043b52
6ae3c5a
1c8a58d
6ae3c5a
 
 
 
 
 
 
 
 
f7e7379
d043b52
 
 
f7e7379
d043b52
 
f7e7379
 
d043b52
 
 
 
f7e7379
d043b52
 
 
 
 
6ae3c5a
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac68de5
 
 
 
d043b52
 
 
 
6ae3c5a
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
 
d043b52
 
 
 
 
 
6ae3c5a
 
 
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae3c5a
 
d043b52
 
6ae3c5a
 
 
 
 
 
 
 
 
 
d043b52
 
 
 
 
 
 
 
 
f7e7379
 
6ae3c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d043b52
 
 
 
 
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
ac68de5
 
 
d043b52
 
 
 
 
 
 
 
6ae3c5a
 
 
 
 
 
 
62d612f
d043b52
 
6ae3c5a
62d612f
d043b52
 
 
 
 
 
 
 
6ae3c5a
 
 
 
 
 
 
 
ac68de5
6ae3c5a
 
d043b52
f7e7379
d043b52
 
 
 
6ae3c5a
 
d043b52
 
6ae3c5a
 
d043b52
6ae3c5a
 
 
 
 
 
d043b52
 
 
 
 
 
f7e7379
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae3c5a
 
d043b52
ac68de5
d043b52
6ae3c5a
 
 
d043b52
 
 
 
6ae3c5a
 
 
 
 
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae3c5a
d043b52
 
 
 
 
 
 
 
 
 
6ae3c5a
d043b52
 
 
 
 
 
 
 
 
6ae3c5a
 
d043b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e7379
 
 
d043b52
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
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
#!/usr/bin/env python
"""Run a plan of calls against the private LTX-2.5 Studio Space under a hard daily GPU budget.

    # DRY RUN (the default): resolve, validate and price every call; contacts nothing
    python scripts/space_matrix.py --plan scripts/space_plan.example.json \\
        --assets-dir ~/assets --quota-ledger ~/space-ledger.jsonl

    # the same plan, executed; when it stops at the ceiling (exit 3), the same command with
    # --resume on the next UTC day runs what is left (calls with an outcome in --out are skipped)
    python scripts/space_matrix.py --plan plan.json --assets-dir ~/assets \\
        --quota-ledger ~/space-ledger.jsonl --out ~/space-runs/matrix --live [--resume]

    # one call from flags; the historical 11-mode loop is --plan default
    python scripts/space_matrix.py --mode text_to_video --width 704 --height 480 --num-frames 49

    # read-only, one GET each, no GPU time
    python scripts/space_matrix.py status
    python scripts/space_matrix.py info
    python scripts/space_matrix.py logs --out run.log

Exit codes: 0 plan complete · 2 illegal plan or contract mismatch (nothing spent) · 3 stopped at
the ceiling · 4 the platform refused on quota · 5 a call failed (with --keep-going the rest of the
plan still runs, and the exit is still 5).

Cost figures are BOUNDS. There is no readable ZeroGPU quota endpoint, so billed seconds cannot be
measured, only bounded: the conservative figure is the wall clock from the moment the Space starts
the event to its completion, times the size multiplier. The ceiling check before each call adds
that call's full reservation (``zerogpu.estimate_duration``, times the multiplier) to today's
conservative total.

The token is read when a command needs it (``HF_TOKEN``, else the Hub's cached token file), is
sent only as the ``Authorization`` header to the Space and the Hub API, and is redacted from
every line, file and error this script writes.
"""

from __future__ import annotations

import argparse
import hashlib
import html
import json
import os
import re
import shutil
import sys
import time
import traceback
import urllib.error
import urllib.parse
import urllib.request
import uuid
from contextlib import contextmanager
from dataclasses import dataclass, field, replace
from dataclasses import fields as dataclass_fields
from datetime import UTC, datetime, timedelta
from pathlib import Path
from typing import Any

REPO = Path(__file__).resolve().parent.parent
# `tests/` for `verify_streams`, the registry-driven stream checker.
for _path in (REPO, REPO / "tests"):
    if str(_path) not in sys.path:
        sys.path.insert(0, str(_path))

import verify_streams  # noqa: E402

from ltx25_studio import constraints, modes, outputs, zerogpu  # noqa: E402
from ltx25_studio.ui import fields  # noqa: E402

__all__ = [
    "API_FIELD_KEYS",
    "DEFAULT_MODE_ORDER",
    "DEFAULT_QUOTA_CEILING_SECONDS",
    "ENDPOINT",
    "EXIT_CALL_FAILED",
    "EXIT_CEILING",
    "EXIT_ILLEGAL",
    "EXIT_OK",
    "EXIT_QUOTA",
    "GATE_ACCEPTED",
    "GATE_DEFERRED",
    "LORA_ENDPOINT",
    "OUTPUT_SLOTS",
    "UNBILLED_CATEGORIES",
    "Call",
    "Ledger",
    "PlanError",
    "QuotaLedgerUnreadable",
    "Secret",
    "app_gate",
    "as_the_space",
    "classify",
    "count_onsets",
    "default_plan",
    "generate_contract_problems",
    "load_plan",
    "main",
    "measure_audio",
    "measure_video",
    "platform_refusal",
    "quota_multiplier",
    "quota_refusal",
    "read_token",
    "redact",
    "reservation_seconds",
    "resume_split",
    "returned_files",
    "schedule_days",
    "utc_day",
    "verify_call",
]

# ---------------------------------------------------------------------------
# the Space and its published signature
# ---------------------------------------------------------------------------

DEFAULT_SPACE_ID = "techfreakworm/LTX2.5-Studio"
DEFAULT_HUB_URL = "https://huggingface.co"
ENDPOINT = "/on_generate"
LORA_ENDPOINT = "/on_validate_lora"
LORA_WEIGHT_ENDPOINT = "/on_lora_weight"
API_PREFIX = "/gradio_api"
#: ``/on_generate``'s file outputs, in order; the report follows them (``[Video, Audio, Markdown]``).
OUTPUT_SLOTS = ("video", "audio")

#: ``/on_generate``'s parameters after ``mode_id``: the field spec minus ``gr.State``, which
#: Gradio does not publish. Read again from ``fields.FIELDS`` at call time (see :func:`api_fields`)
#: so a field appended after import is still sent.
API_FIELD_KEYS: tuple[str, ...] = tuple(f.key for f in fields.FIELDS if f.kind != "state")

#: Field kind -> the component name Gradio publishes. Only ``text`` differs from its kind.
_COMPONENT_FOR_KIND = {"text": "textbox"}

# ---------------------------------------------------------------------------
# budget
# ---------------------------------------------------------------------------

DAILY_QUOTA_SECONDS = 2400.0
DEFAULT_QUOTA_CEILING_SECONDS = 1920.0
#: The Space's ``LTX25_ZEROGPU_SIZE``; ``zerogpu.quota_multiplier`` turns it into the quota factor.
DEFAULT_ZEROGPU_SIZE = "xlarge"
DEFAULT_CALL_TIMEOUT_SECONDS = 1800.0
DEFAULT_READ_TIMEOUT_SECONDS = 120.0

EXIT_OK = 0
EXIT_ILLEGAL = 2
EXIT_CEILING = 3
EXIT_QUOTA = 4
EXIT_CALL_FAILED = 5

#: How the ZeroGPU platform words its refusals (``spaces`` ``zero/client.py``, 0.50-0.52): each
#: error's title and the message under it. The app's failure card quotes only the MESSAGE; a
#: title reaches the API only when an error escapes the handler. Checked in this order.
_PLATFORM_REFUSALS: tuple[tuple[str, re.Pattern], ...] = (
    # the PRO/free/IP quota, the runs limit, the Space-wide limit, pending credits
    ("quota", re.compile(
        r"ZeroGPU quota exceeded"
        r"|exceeded your [\w ]*?ZeroGPU quota"
        r"|exceeded your ZeroGPU runs limit"
        r"|exceeded [\w ]*GPU quota"
        r"|reached its GPU limit"
        r"|ZeroGPU pending credits exceeded"
        r"|too many ZeroGPU credits allocated",
        re.IGNORECASE,
    )),
    # the reservation is more than one call may ask for; nothing ran
    ("limit", re.compile(
        r"ZeroGPU illegal duration|requested GPU duration \([\d.]+s\) is larger than the maximum allowed",
        re.IGNORECASE,
    )),
    # no GPU was free in time; nothing ran
    ("queue", re.compile(r"ZeroGPU queue timeout|No GPU was available", re.IGNORECASE)),
)

#: Failure categories by their string values (``ltx25_studio.errors``). These two mean the
#: platform refused before a GPU was used: recorded with zero billed seconds.
UNBILLED_CATEGORIES = ("limit", "queue")

# ---------------------------------------------------------------------------
# content thresholds (measurements, reported with their thresholds; never stop a plan)
# ---------------------------------------------------------------------------

MIN_PIXEL_STD = 1.0
MIN_DISTINCT_FRAMES = 2
#: mean |Δ| over the frames' own spatial std; frozen probes measured <= 0.000523, the stillest
#: real post-fix render 0.004948.
MIN_RELATIVE_FRAME_DELTA = 0.001
LOW_MEAN_FRAME_DELTA = 0.5

#: The pinned vocoder's rate; ``expect.sample_rate`` overrides it per call.
PINNED_VOCODER_SAMPLING_RATE = 48000

# ---------------------------------------------------------------------------
# the historical default plan (--plan default)
# ---------------------------------------------------------------------------

CHEAPEST_SIZE = modes.MIN_RESOLUTION
CHEAPEST_FRAMES = 9
SEED = 1234
PROMPT = (
    "A red sports car driving fast along a winding coastal road at sunset, "
    "waves breaking on rocks below, cinematic camera following alongside"
)

#: Producers first: text_to_video makes the video (and, by frame, the image) the conditioned
#: modes chain from; text_to_audio makes the audio. Each chain is explicit in the plan.
DEFAULT_MODE_ORDER: tuple[str, ...] = (
    "text_to_video",
    "text_to_audio",
    "text_to_audio_video",
    "image_to_video",
    "image_text_to_video",
    "image_to_audio_video",
    "image_text_to_audio_video",
    "video_to_video",
    "video_to_audio",
    "audio_to_audio",
    "audio_to_video",
)

MEDIA_KINDS = ("image", "video", "audio")
_CALL_KEYS = frozenset({"label", "mode", "loras", "expect", "note"})
_EXPECT_KEYS = {
    "width": int,
    "height": int,
    "frames": int,
    "has_audio": bool,
    "has_video": bool,
    "sample_rate": int,
    "frame_rate": float,
}
_LABEL = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$")
_HUB_ID = re.compile(r"^[A-Za-z0-9][\w.-]*/[\w.-]+(/[\w./-]+)?$")


# ---------------------------------------------------------------------------
# clocks (module-level so tests can drive them)
# ---------------------------------------------------------------------------


def _utcnow() -> datetime:
    return datetime.now(UTC)


def _monotonic() -> float:
    return time.monotonic()


def utc_day(now: datetime | None = None) -> str:
    return (now or _utcnow()).strftime("%Y-%m-%d")


def next_utc_midnight(now: datetime | None = None) -> datetime:
    moment = now or _utcnow()
    return datetime(moment.year, moment.month, moment.day, tzinfo=UTC) + timedelta(days=1)


# ---------------------------------------------------------------------------
# the token
# ---------------------------------------------------------------------------

_SECRETS: list[str] = []
REDACTED = "<redacted>"


class Secret:
    """The token. Never printed, pickled or formatted; :meth:`reveal` is read by the transport."""

    __slots__ = ("_value",)

    def __init__(self, value: str):
        self._value = value
        if value and value not in _SECRETS:
            _SECRETS.append(value)

    def reveal(self) -> str:
        return self._value

    def __repr__(self) -> str:
        return f"Secret({REDACTED})"

    __str__ = __repr__

    def __reduce__(self):
        raise TypeError("a Secret is not picklable")


def token_path() -> Path:
    if os.environ.get("HF_TOKEN_PATH"):
        return Path(os.environ["HF_TOKEN_PATH"]).expanduser()
    home = os.environ.get("HF_HOME") or "~/.cache/huggingface"
    return Path(home).expanduser() / "token"


def read_token() -> Secret | None:
    """``HF_TOKEN``, else the cached token file. Read at call time, never at import."""
    value = (os.environ.get("HF_TOKEN") or "").strip()
    if not value:
        try:
            value = token_path().read_text().strip()
        except OSError:
            value = ""
    return Secret(value) if value else None


def redact(text: Any) -> str:
    out = "" if text is None else str(text)
    for secret in _SECRETS:
        out = out.replace(secret, REDACTED)
    return out


def _redacted_json(payload: Any) -> str:
    return redact(json.dumps(payload, indent=2, sort_keys=False, default=str)) + "\n"


# ---------------------------------------------------------------------------
# errors
# ---------------------------------------------------------------------------


class PlanError(ValueError):
    """The plan cannot run as written. Nothing was sent."""


class ContractMismatch(RuntimeError):
    """The live Space's published signature is not what the local field spec builds."""


class QuotaLedgerUnreadable(RuntimeError):
    """The ledger exists and is not a ledger. Refused rather than read as zero."""


class HttpError(RuntimeError):
    def __init__(self, code: int, url: str, body: str):
        self.code = code
        super().__init__(redact(f"HTTP {code} from {_strip_query(url)}: {body[:500]}"))


class TransportError(RuntimeError):
    pass


class CallTimeout(RuntimeError):
    pass


def _strip_query(url: str) -> str:
    return url.split("?", 1)[0]


# ---------------------------------------------------------------------------
# HTTP
# ---------------------------------------------------------------------------


def _origin(url: str) -> str:
    parts = urllib.parse.urlsplit(url)
    return f"{parts.scheme}://{parts.netloc}".lower()


class Http:
    """urllib with the token attached only to trusted origins and never carried across a redirect."""

    def __init__(self, secret: Secret | None, trusted: tuple[str, ...] = ()):
        self._secret = secret
        self._trusted = {_origin(u) for u in trusted}
        self._opener = urllib.request.build_opener()

    def trust(self, url: str) -> None:
        self._trusted.add(_origin(url))

    def open(self, method: str, url: str, *, data: bytes | None = None, headers=None, timeout: float = 60.0):
        request = urllib.request.Request(url, data=data, method=method, headers=dict(headers or {}))
        if self._secret is not None and _origin(url) in self._trusted:
            # Unredirected: urllib drops it on any redirect, to any host.
            request.add_unredirected_header("Authorization", f"Bearer {self._secret.reveal()}")
        try:
            return self._opener.open(request, timeout=timeout)
        except urllib.error.HTTPError as exc:
            try:
                body = exc.read(2000).decode("utf-8", "replace")
            except Exception:  # noqa: BLE001
                body = ""
            raise HttpError(exc.code, url, body) from None
        except (urllib.error.URLError, OSError) as exc:
            raise TransportError(redact(f"{type(exc).__name__} reaching {_strip_query(url)}: {exc}")) from None

    def get_json(self, url: str, *, timeout: float = 60.0) -> Any:
        with self.open("GET", url, headers={"Accept": "application/json"}, timeout=timeout) as response:
            return json.loads(response.read().decode("utf-8"))

    def post_json(self, url: str, payload: Any, *, timeout: float = 60.0) -> Any:
        body = json.dumps(payload).encode()
        headers = {"Content-Type": "application/json", "Accept": "application/json"}
        with self.open("POST", url, data=body, headers=headers, timeout=timeout) as response:
            return json.loads(response.read().decode("utf-8"))


def iter_sse(response):
    """``data:`` payloads of an event stream, one parsed JSON value per event."""
    buffer: list[str] = []
    while True:
        raw = response.readline()
        if not raw:
            if buffer:
                yield json.loads("\n".join(buffer))
            return
        line = raw.decode("utf-8", "replace").rstrip("\r\n")
        if not line:
            if buffer:
                text = "\n".join(buffer)
                buffer = []
                yield json.loads(text)
            continue
        if line.startswith("data:"):
            buffer.append(line[5:].lstrip())


def space_host(space_id: str) -> str:
    """``owner/Name.x`` -> ``https://owner-name-x.hf.space``, the Space's own subdomain."""
    slug = re.sub(r"[^a-z0-9]+", "-", space_id.lower()).strip("-")
    return f"https://{slug}.hf.space"


class SpaceApp:
    """The Gradio app's HTTP surface: config, info, upload, queue, files."""

    def __init__(self, base_url: str, http: Http, *, api_prefix: str = API_PREFIX):
        self.base = base_url.rstrip("/")
        self.http = http
        self.prefix = "/" + api_prefix.strip("/") if api_prefix else ""
        http.trust(self.base)

    def url(self, path: str) -> str:
        return f"{self.base}{self.prefix}{path}"

    def config(self) -> dict:
        return self.http.get_json(f"{self.base}/config")

    def info(self) -> dict:
        return self.http.get_json(self.url("/info"))

    def upload(self, local: Path) -> dict:
        boundary = uuid.uuid4().hex
        content = Path(local).read_bytes()
        name = Path(local).name
        body = (
            f"--{boundary}\r\nContent-Disposition: form-data; name=\"files\"; filename=\"{name}\"\r\n"
            f"Content-Type: application/octet-stream\r\n\r\n"
        ).encode() + content + f"\r\n--{boundary}--\r\n".encode()
        headers = {"Content-Type": f"multipart/form-data; boundary={boundary}", "Accept": "application/json"}
        with self.http.open("POST", self.url("/upload"), data=body, headers=headers, timeout=600) as response:
            paths = json.loads(response.read().decode("utf-8"))
        return {"path": paths[0], "orig_name": name, "meta": {"_type": "gradio.FileData"}}

    def join(self, fn_index: int, data: list, session_hash: str) -> str:
        payload = {
            "data": data,
            "event_data": None,
            "fn_index": fn_index,
            "trigger_id": None,
            "session_hash": session_hash,
        }
        return str(self.http.post_json(self.url("/queue/join"), payload)["event_id"])

    def stream(self, session_hash: str, *, read_timeout: float):
        query = urllib.parse.urlencode({"session_hash": session_hash})
        return self.http.open(
            "GET", self.url(f"/queue/data?{query}"), headers={"Accept": "text/event-stream"}, timeout=read_timeout
        )

    def download(self, server_path: str, dest: Path) -> Path:
        url = self.url("/file=" + urllib.parse.quote(server_path, safe="/:"))
        dest.parent.mkdir(parents=True, exist_ok=True)
        with self.http.open("GET", url, timeout=600) as response, dest.open("wb") as handle:
            shutil.copyfileobj(response, handle)
        return dest


class Hub:
    """The Hub API's read-only Space endpoints."""

    def __init__(self, hub_url: str, http: Http):
        self.base = hub_url.rstrip("/")
        self.http = http
        http.trust(self.base)

    def space(self, space_id: str) -> dict:
        return self.http.get_json(f"{self.base}/api/spaces/{space_id}")

    def logs(self, space_id: str, *, kind: str, max_seconds: float) -> list[str]:
        lines: list[str] = []
        deadline = _monotonic() + max_seconds
        url = f"{self.base}/api/spaces/{space_id}/logs/{kind}"
        try:
            with self.http.open("GET", url, headers={"Accept": "text/event-stream"}, timeout=min(max_seconds, 10.0)) as response:
                for event in iter_sse(response):
                    if isinstance(event, dict):
                        lines.append(f"{event.get('timestamp', '')} {event.get('data', '')}".strip())
                    else:
                        lines.append(str(event))
                    if _monotonic() >= deadline:
                        break
        except TimeoutError:
            pass
        except TransportError as exc:
            if "timed out" not in str(exc):
                raise
        return lines


# ---------------------------------------------------------------------------
# the plan
# ---------------------------------------------------------------------------


def api_fields() -> tuple:
    """Non-state fields, from the spec as it stands now."""
    return tuple(f for f in fields.FIELDS if f.kind != "state")


def _field(key: str):
    return next((f for f in fields.FIELDS if f.key == key), None)


def _accepts(spec, kind: str) -> bool:
    predicate = getattr(modes, "accepts", None)
    return bool(predicate(spec, kind)) if predicate else kind in spec.inputs


def _requires(spec, kind: str) -> bool:
    predicate = getattr(modes, "requires", None)
    return bool(predicate(spec, kind)) if predicate else kind in spec.inputs


def _skip_stage_2(spec) -> bool:
    return bool(getattr(spec, "skip_stage_2", False))


def _varies_clip(spec) -> bool:
    """The clip is a variation source (mode 04): its length is the run's length."""
    return getattr(spec, "video_conditioning", None) == "variation"


def _varies_audio(spec) -> bool:
    return getattr(spec, "audio_conditioning", None) == "variation"


@dataclass(frozen=True)
class MediaInput:
    kind: str
    path: Path | None = None
    from_call: str | None = None
    #: image chains only: a frame index, ``"middle"`` or ``"last"``.
    frame: int | str | None = None

    @property
    def chained(self) -> bool:
        return self.from_call is not None

    def describe(self) -> dict:
        if self.chained:
            out = {"chained": True, "from_call": self.from_call}
            if self.kind == "image":
                out["frame"] = self.frame
            return out
        size = self.path.stat().st_size if self.path and self.path.exists() else None
        return {"chained": False, "path": str(self.path), "bytes": size}


#: ``lora.staging.DEFAULT_WEIGHT``: the strength ``/on_validate_lora`` stages an adapter at.
#: Read lazily (the LoRA package imports the Hub client); 1.0 where it cannot be read.
_STAGED_WEIGHT: list[float] = []


def staged_lora_weight() -> float:
    if not _STAGED_WEIGHT:
        try:
            from ltx25_studio.lora import staging

            _STAGED_WEIGHT.append(float(staging.DEFAULT_WEIGHT))
        except Exception:  # noqa: BLE001
            _STAGED_WEIGHT.append(1.0)
    return _STAGED_WEIGHT[0]


@dataclass(frozen=True)
class LoraInput:
    source: str
    weight: float = 1.0
    local: Path | None = None

    @property
    def needs_weight_route(self) -> bool:
        """A strength other than the one ``/on_validate_lora`` stages at."""
        return self.weight != staged_lora_weight()

    @property
    def label(self) -> str:
        return self.local.name if self.local else self.source

    def describe(self) -> dict:
        return {
            "source": self.source,
            "route": "upload" if self.local else "hub",
            "local": str(self.local) if self.local else None,
            "weight": self.weight,
        }


#: ``Call.gate``: the app's own gate accepted the call as the Space would run it, or the call
#: takes an input an earlier call produces, so the gate runs when that file exists.
GATE_ACCEPTED = "accepted"
GATE_DEFERRED = "deferred"


@dataclass(frozen=True)
class Call:
    label: str
    mode: str
    values: dict
    media: dict
    loras: tuple = ()
    expect: dict = field(default_factory=dict)
    note: str = ""
    #: :data:`GATE_ACCEPTED`, :data:`GATE_DEFERRED`, or "" before the gate was asked.
    gate: str = ""
    #: From ``generate.prepare_request``: the frame count the app will run (None: the model
    #: chooses) and the notes it will carry ("length set from your audio ...").
    app_frames: int | None = None
    app_notes: tuple = ()
    #: ``(key, sent, what the app runs)`` for every field the app's scoping changes.
    app_changes: tuple = ()
    #: The variation level the app snaps ``noise_scale`` to (modes 04 and 10), else None.
    app_level: float | None = None

    @property
    def spec(self):
        return modes.get_mode(self.mode)

    def value(self, key: str) -> Any:
        if key in self.values:
            return self.values[key]
        spec = _field(key)
        return None if spec is None else spec.default

    def runs(self, key: str) -> Any:
        """The value the app runs with: after its scoping when the gate ran, else as sent."""
        for name, _sent, ran in self.app_changes:
            if name == key:
                return ran
        return self.value(key)

    @property
    def chained(self) -> tuple[str, ...]:
        return tuple(kind for kind, item in self.media.items() if item.chained)

    @property
    def min_resolution(self) -> bool:
        return bool(getattr(self.spec, "min_resolution", False))

    @property
    def render_size(self) -> tuple[int, int]:
        if self.min_resolution:
            return (modes.MIN_RESOLUTION, modes.MIN_RESOLUTION)
        return (int(self.value("width")), int(self.value("height")))

    @property
    def stage_2(self) -> bool:
        return bool(self.value("enable_stage_2")) and not _skip_stage_2(self.spec)

    @property
    def exports(self) -> tuple[str, ...]:
        return tuple(self.spec.exports)

    def expected_geometry(self) -> tuple[int, int] | None:
        if "video" not in self.exports:
            return None
        width, height = self.render_size
        if self.stage_2:
            width, height = 2 * width, 2 * height
        return (int(self.expect.get("width", width)), int(self.expect.get("height", height)))

    def expected_frames(self) -> int | None:
        if "frames" in self.expect:
            return int(self.expect["frames"])
        if "video" not in self.exports:
            return None
        if self.app_frames is not None:
            return self.app_frames
        if self.value("auto_duration") or _varies_clip(self.spec):
            # The model, the audio or the clip sets the length: known once the gate has run.
            return None
        return int(self.value("num_frames"))


def _check_value(spec, value, problems: list[str], where: str) -> Any:
    """``value`` as it is sent; a choice may be named by its label ("Sharper (standard decoder)")."""
    kind = spec.kind
    if kind == "text" and not isinstance(value, str):
        problems.append(f"{where}: {spec.key} must be text")
    elif kind == "checkbox" and not isinstance(value, bool):
        problems.append(f"{where}: {spec.key} must be true or false")
    elif kind in ("number", "slider"):
        if isinstance(value, bool) or not isinstance(value, (int, float)):
            problems.append(f"{where}: {spec.key} must be a number")
            return value
        low, high = spec.options.get("minimum"), spec.options.get("maximum")
        if low is not None and value < low or high is not None and value > high:
            problems.append(f"{where}: {spec.key}={value} is outside {low}..{high}")
    elif kind in ("dropdown", "radio"):
        choices = spec.options.get("choices")
        if choices:
            pairs = [tuple(c) if isinstance(c, (list, tuple)) else (c, c) for c in choices]
            by_label = {label: sent for label, sent in pairs}
            allowed = [sent for _label, sent in pairs]
            if value not in allowed and value in by_label:
                return by_label[value]
            if value not in allowed:
                shown = ", ".join(f"{sent!r} ({label})" if label != sent else repr(sent) for label, sent in pairs)
                problems.append(f"{where}: {spec.key}={value!r} is not one of {shown}")
    elif kind == "dataframe" and not isinstance(value, list):
        problems.append(f"{where}: {spec.key} must be a list of rows")
    return value


def _resolve_path(raw: str, base: Path) -> Path:
    path = Path(raw).expanduser()
    return path if path.is_absolute() else (base / path)


def _parse_call(raw: dict, *, index: int, base: Path, earlier: dict, problems: list[str]) -> Call | None:
    before = len(problems)
    where = f"call {index + 1}"
    if not isinstance(raw, dict):
        problems.append(f"{where}: must be an object")
        return None
    label = raw.get("label")
    if not isinstance(label, str) or not _LABEL.match(label):
        problems.append(f"{where}: 'label' must match {_LABEL.pattern} (it names the evidence folder)")
        return None
    where = f"call {label!r}"
    if label in earlier:
        problems.append(f"{where}: the label is used twice")
        return None
    try:
        spec = modes.get_mode(str(raw.get("mode")))
    except Exception as exc:  # noqa: BLE001
        problems.append(f"{where}: {exc}")
        return None

    values: dict = {}
    media: dict = {}
    loras: list[LoraInput] = []
    expect: dict = {}
    for key, value in raw.items():
        if key in ("label", "mode", "note"):
            continue
        if key == "expect":
            if not isinstance(value, dict):
                problems.append(f"{where}: 'expect' must be an object")
                continue
            for name, wanted in value.items():
                kind = _EXPECT_KEYS.get(name)
                if kind is None:
                    problems.append(f"{where}: expect.{name} is not one of {sorted(_EXPECT_KEYS)}")
                elif kind is bool and not isinstance(wanted, bool):
                    problems.append(f"{where}: expect.{name} must be true or false")
                elif kind is not bool and (isinstance(wanted, bool) or not isinstance(wanted, (int, float))):
                    problems.append(f"{where}: expect.{name} must be a number")
                else:
                    expect[name] = wanted
            continue
        if key == "loras":
            if not isinstance(value, list):
                problems.append(f"{where}: 'loras' must be a list of {{source, weight}}")
                continue
            for entry in value:
                lora = _parse_lora(entry, base=base, where=where, problems=problems)
                if lora is not None:
                    loras.append(lora)
            continue
        spec_field = _field(key)
        if spec_field is None:
            known = ", ".join(f.key for f in api_fields())
            problems.append(
                f"{where}: {key!r} is not a field of this app's {ENDPOINT} (fields: {known}; "
                f"call keys: {', '.join(sorted(_CALL_KEYS))})"
            )
            continue
        if spec_field.kind == "state":
            problems.append(f"{where}: {key!r} is staged through {LORA_ENDPOINT}; use the call's 'loras' list")
            continue
        if spec_field.kind in MEDIA_KINDS:
            item = _parse_media(spec_field.kind, value, base=base, where=where, earlier=earlier, problems=problems)
            if item is not None:
                media[spec_field.kind] = item
            continue
        values[key] = _check_value(spec_field, value, problems, where)

    call = Call(label=label, mode=spec.id, values=values, media=media, loras=tuple(loras), expect=expect,
                note=str(raw.get("note", "")))

    for kind in MEDIA_KINDS:
        if kind in media and not _accepts(spec, kind):
            problems.append(f"{where}: {spec.id} takes no {kind}")
        if kind not in media and _requires(spec, kind):
            problems.append(
                f"{where}: {spec.id} needs a {kind}: give a file under --assets-dir, or "
                f'{{"from_call": "<earlier label>"}} to use an earlier call\'s output'
            )
    if str(values.get("prompt", "") or "").strip() and not _accepts(spec, "text"):
        problems.append(f"{where}: {spec.id} takes no prompt; remove 'prompt'")
    try:
        width, height = call.render_size
        constraints.validate_dimensions(width, height)
        constraints.validate_dimension_limit(width, name="width")
        constraints.validate_dimension_limit(height, name="height")
    except Exception as exc:  # noqa: BLE001
        problems.append(f"{where}: {exc}")
    try:
        if not call.value("auto_duration"):
            constraints.validate_num_frames(int(call.value("num_frames")))
            constraints.validate_num_frames_limit(int(call.value("num_frames")))
        else:
            if float(call.value("min_seconds")) >= float(call.value("max_seconds")):
                problems.append(f"{where}: max_seconds must be greater than min_seconds")
            constraints.validate_seconds_limit(float(call.value("max_seconds")))
    except Exception as exc:  # noqa: BLE001
        problems.append(f"{where}: {exc}")
    if len(problems) > before:
        return call
    if call.chained:
        # The app's gate opens its inputs; a file an earlier call produces does not exist yet.
        # It runs on that file, before anything is sent, once the producer has run.
        return replace(call, gate=GATE_DEFERRED)
    try:
        return gated(call, {kind: item.path for kind, item in media.items()})
    except PlanError as exc:
        problems.append(f"{where}: {exc}")
    except Exception as exc:  # noqa: BLE001 -- the app's own words
        problems.append(f"{where}: the app would refuse this call before reserving a GPU: {exc}")
    return call


@dataclass(frozen=True)
class Gate:
    """What ``generate.prepare_request`` said about one call."""

    frames: int | None
    notes: tuple[str, ...]
    changes: tuple
    level: float | None


class _Platform:
    """Stands for the ``spaces`` package the Space imports: ``zerogpu.is_zerogpu`` says yes."""

    is_stub = False


@contextmanager
def as_the_space():
    """``device.current()`` answers as on the Space (ZeroGPU, CUDA) while the app's gate runs.

    The gate refuses some requests by device (stage 2, and the diffusion decoder off CUDA); left
    alone it would apply THIS machine's ``LTX25_DEVICE`` to a call that runs on the Space.
    """
    from ltx25_studio import config, device

    resolve = getattr(device, "resolve", None)
    if resolve is None or not hasattr(device, "current"):
        yield None
        return
    try:
        settings = replace(config.load_config(), device="auto")
        resolution = resolve(spaces_module=_Platform(), runtime_config=settings)
    except Exception as exc:  # noqa: BLE001 -- a local setting, not the Space's
        raise PlanError(
            f"the app's gate cannot be run as the Space runs it, because this machine's LTX25_* "
            f"settings do not load: {exc}"
        ) from None
    original = device.current
    device.current = lambda: resolution
    try:
        yield resolution
    finally:
        device.current = original


def _space_input(kind: str, path) -> Any:
    """A media file as the Space's handler receives it: images as PIL (``gr.Image(type="pil")``),
    video and audio as file paths."""
    if kind != "image":
        return str(path)
    from PIL import Image

    try:
        with Image.open(path) as image:
            return image.convert("RGB")
    except Exception as exc:  # noqa: BLE001
        raise PlanError(
            f"image {Path(path).name} could not be read as a picture ({type(exc).__name__}); the "
            f"Space's image input would refuse it"
        ) from None


def _app_request(call: Call, media: dict):
    """The ``GenerationRequest`` the Space's handler would build from this call's fields."""
    from ltx25_studio import generate

    known = {f.name for f in dataclass_fields(generate.GenerationRequest)}
    width, height = call.render_size
    kwargs: dict = {"mode": call.mode}
    for spec in api_fields():
        if spec.key not in known:
            continue
        if spec.kind in MEDIA_KINDS:
            path = media.get(spec.kind)
            kwargs[spec.key] = None if path is None else _space_input(spec.kind, path)
        elif spec.key in ("width", "height"):
            kwargs[spec.key] = width if spec.key == "width" else height
        elif spec.kind == "dataframe":
            kwargs[spec.key] = tuple(call.value(spec.key) or ())
        else:
            kwargs[spec.key] = call.value(spec.key)
    return generate.GenerationRequest(**kwargs)


def _variation_level(spec, scoped) -> float | None:
    """The level a variation mode runs at, as the mode's own ``build_kwargs`` decides it (the
    nearest supported level to ``noise_scale``, or the mode's default); None for other modes."""
    if not (_varies_clip(spec) or _varies_audio(spec)):
        return None
    kwargs = spec.build_kwargs(scoped)
    clip = kwargs.get("video_variation")
    level = getattr(clip, "level", None) if clip is not None else kwargs.get("audio_noise_level")
    return None if level is None else float(level)


def app_gate(call: Call, media: dict) -> Gate:
    """``generate.prepare_request``, the checks the app runs before it reserves a GPU, run as
    the Space runs them (:func:`as_the_space`), on the files themselves.

    Raises what the Space would refuse the call with.
    """
    from ltx25_studio import generate

    request = _app_request(call, media)
    with as_the_space():
        scoped, spec, notes = generate.prepare_request(request)
    changes = []
    for key in (f.key for f in api_fields() if f.kind not in MEDIA_KINDS):
        if not hasattr(request, key):
            continue
        sent, ran = getattr(request, key), getattr(scoped, key)
        if sent != ran:
            changes.append((key, sent, ran))
    return Gate(
        frames=None if scoped.auto_duration else int(scoped.num_frames),
        notes=tuple(str(note) for note in notes),
        changes=tuple(changes),
        level=_variation_level(spec, scoped),
    )


def gated(call: Call, media: dict) -> Call:
    """``call`` with what the app's gate said; raises its refusal."""
    gate = app_gate(call, media)
    return replace(call, gate=GATE_ACCEPTED, app_frames=gate.frames, app_notes=gate.notes,
                   app_changes=gate.changes, app_level=gate.level)


def _parse_media(kind, value, *, base, where, earlier, problems) -> MediaInput | None:
    if isinstance(value, str):
        path = _resolve_path(value, base)
        if not path.is_file():
            problems.append(f"{where}: {kind} file {path} does not exist")
            return None
        return MediaInput(kind=kind, path=path)
    if isinstance(value, dict) and "from_call" in value:
        source = value["from_call"]
        extra = set(value) - {"from_call", "frame"}
        if extra:
            problems.append(f"{where}: {kind}: unknown key(s) {sorted(extra)}")
        producer = earlier.get(source)
        if producer is None:
            problems.append(f"{where}: {kind} from_call {source!r} is not an earlier call in this plan")
            return None
        track = "audio" if kind == "audio" else "video"
        if track not in producer.exports:
            problems.append(f"{where}: {kind} from_call {source!r}: {producer.mode} exports no {track}")
            return None
        frame = value.get("frame", "middle") if kind == "image" else None
        if kind == "image" and not (isinstance(frame, int) and not isinstance(frame, bool) and frame >= 0
                                    or frame in ("middle", "last")):
            problems.append(f"{where}: image frame must be an index >= 0, 'middle' or 'last'")
            return None
        if kind != "image" and "frame" in value:
            problems.append(f"{where}: 'frame' applies to an image chain only")
        return MediaInput(kind=kind, from_call=source, frame=frame)
    problems.append(f"{where}: {kind} must be a file path or {{\"from_call\": \"<label>\"}}")
    return None


def _parse_lora(entry, *, base, where, problems) -> LoraInput | None:
    if isinstance(entry, str):
        entry = {"source": entry}
    if not isinstance(entry, dict) or not isinstance(entry.get("source"), str):
        problems.append(f"{where}: each LoRA needs a 'source' (a Hub repo id or a local .safetensors)")
        return None
    extra = set(entry) - {"source", "weight"}
    if extra:
        problems.append(f"{where}: LoRA: unknown key(s) {sorted(extra)}")
    weight = entry.get("weight", staged_lora_weight())
    if isinstance(weight, bool) or not isinstance(weight, (int, float)) or weight <= 0:
        problems.append(f"{where}: LoRA weight must be a positive number")
        return None
    if weight != staged_lora_weight():
        try:
            from ltx25_studio.lora import staging

            staging.check_weight(weight, source=entry["source"])
        except ImportError:
            pass
        except Exception as exc:  # noqa: BLE001 -- the app's own words
            problems.append(f"{where}: {exc}")
            return None
    source = entry["source"].strip()
    candidate = _resolve_path(source, base)
    if source.endswith(".safetensors") and candidate.is_file():
        return LoraInput(source=source, weight=float(weight), local=candidate)
    if _HUB_ID.match(source) and not source.startswith((".", "/", "~")):
        return LoraInput(source=source, weight=float(weight))
    problems.append(f"{where}: LoRA {source!r} is neither a file under --assets-dir nor a Hub repo id")
    return None


def parse_plan(document: Any, *, base: Path) -> list[Call]:
    problems: list[str] = []
    defaults: dict = {}
    if isinstance(document, dict):
        unknown = set(document) - {"defaults", "calls", "note"}
        if unknown:
            problems.append(f"plan: unknown top-level key(s) {sorted(unknown)}")
        defaults = document.get("defaults") or {}
        entries = document.get("calls")
        if not isinstance(defaults, dict):
            problems.append("plan: 'defaults' must be an object")
            defaults = {}
    else:
        entries = document
    if not isinstance(entries, list) or not entries:
        raise PlanError("the plan must be a non-empty list of calls (or {'defaults': {...}, 'calls': [...]})")
    calls: dict[str, Call] = {}
    for index, raw in enumerate(entries):
        merged = {**defaults, **raw} if isinstance(raw, dict) else raw
        if isinstance(raw, dict) and isinstance(defaults.get("expect"), dict) and isinstance(raw.get("expect"), dict):
            merged["expect"] = {**defaults["expect"], **raw["expect"]}
        call = _parse_call(merged, index=index, base=base, earlier=calls, problems=problems)
        if call is not None:
            calls[call.label] = call
    if problems:
        raise PlanError("\n".join(problems))
    return list(calls.values())


def load_plan(path: str | Path, *, assets_dir: Path | None = None) -> list[Call]:
    plan_path = Path(path).expanduser()
    try:
        document = json.loads(plan_path.read_text())
    except (OSError, ValueError) as exc:
        raise PlanError(f"{plan_path}: {exc}") from None
    return parse_plan(document, base=assets_dir or plan_path.resolve().parent)


def default_plan(mode_ids=None, overrides: dict | None = None) -> list[Call]:
    """The historical 11-mode loop, with every chain written out."""
    chosen = list(DEFAULT_MODE_ORDER if mode_ids is None else mode_ids)
    entries: list[dict] = []
    labels: dict[str, int] = {}
    producers: dict[str, str] = {}
    problems: list[str] = []
    for mode_id in chosen:
        try:
            spec = modes.get_mode(mode_id)
        except Exception as exc:  # noqa: BLE001
            raise PlanError(str(exc)) from None
        labels[mode_id] = labels.get(mode_id, 0) + 1
        label = mode_id if labels[mode_id] == 1 else f"{mode_id}-{labels[mode_id]}"
        entry: dict = {"label": label, "mode": spec.id, "width": CHEAPEST_SIZE, "height": CHEAPEST_SIZE,
                       "num_frames": CHEAPEST_FRAMES, "seed": SEED}
        if _accepts(spec, "text"):
            entry["prompt"] = PROMPT
        entry.update(overrides or {})
        if not _accepts(spec, "text"):
            entry.pop("prompt", None)
        for kind in MEDIA_KINDS:
            if not _requires(spec, kind):
                continue
            track = "audio" if kind == "audio" else "video"
            source = producers.get(track)
            if source is None:
                problems.append(f"{label} needs a {kind}, and nothing before it in this plan produces {track}")
                continue
            entry[kind] = {"from_call": source, "frame": "middle"} if kind == "image" else {"from_call": source}
        for track in spec.exports:
            producers.setdefault(track, label)
        entries.append(entry)
    if problems:
        raise PlanError("\n".join(problems))
    return parse_plan(entries, base=Path.cwd())


# ---------------------------------------------------------------------------
# what a call costs
# ---------------------------------------------------------------------------


def quota_multiplier(size: str = DEFAULT_ZEROGPU_SIZE) -> float:
    """The app's own quota factor for the Space's GPU size."""
    return float(zerogpu.quota_multiplier(size))


def _priced_request(call: Call):
    """The request the Space's estimator sees for this call: its fields as the app runs them,
    with the length the app's gate set (a recording's or a clip's) once the gate has run."""
    from ltx25_studio import generate

    width, height = call.render_size
    fields = {
        "mode": call.mode,
        "width": width,
        "height": height,
        "num_frames": int(call.runs("num_frames")),
        "auto_duration": bool(call.runs("auto_duration")),
        "min_seconds": float(call.runs("min_seconds")),
        "max_seconds": float(call.runs("max_seconds")),
        "noise_scale": call.app_level if call.app_level is not None else call.runs("noise_scale"),
    }
    if call.app_frames is not None:
        fields.update(num_frames=int(call.app_frames), auto_duration=False)
        if _varies_clip(call.spec):
            fields["extras"] = {"length_source": generate.LENGTH_FROM_VIDEO}
    return generate.GenerationRequest(**fields)


def reservation_seconds(call: Call, *, floor_seconds: int | None = None) -> int:
    """The app's own estimator for the request as the app runs it: its size and length, with
    the Space's ``LTX25_GPU_DURATION_FLOOR_SECONDS`` as ``floor_seconds`` (unset by default)."""
    return int(
        zerogpu.estimate_duration(
            request=_priced_request(call),
            enable_stage_2=call.stage_2 and bool(call.runs("enable_stage_2")),
            loras=[lora.describe() for lora in call.loras] or None,
            enhance_prompt=bool(call.runs("enhance_prompt")),
            gpu_duration_floor_seconds=floor_seconds,
        )
    )


def _reservation_parts(call: Call, *, floor_seconds: int | None) -> list[str]:
    """What the reservation is made of, in the estimator's own terms."""
    request = _priced_request(call)
    shape = zerogpu.run_shape(request=request)
    plain = {"request": request, "loras": None, "enhance_prompt": False, "gpu_duration_floor_seconds": None}
    run = zerogpu.estimate_duration(enable_stage_2=False, **plain)
    parts = [f"{run} for {shape.width}x{shape.height}x{shape.frames}"]
    if call.stage_2 and call.runs("enable_stage_2"):
        parts.append(f"+{zerogpu.estimate_duration(enable_stage_2=True, **plain) - run} Upscale")
    if call.loras:
        parts.append(f"+{zerogpu.LORA_DURATION_SECONDS} LoRA")
    if call.runs("enhance_prompt"):
        parts.append(f"+{zerogpu.ENHANCE_DURATION_SECONDS} enhancer")
    if floor_seconds:
        parts.append(f"at least {floor_seconds}")
    return parts


def platform_refusal(*texts: Any) -> tuple[str, str] | None:
    """``(category, the platform's words)`` for the first refusal wording in ``texts``, or ``None``.

    Quota is looked for first, in every text, because it ends the whole invocation.
    """
    plain = [html.unescape(str(text)) for text in texts if text]
    for category, pattern in _PLATFORM_REFUSALS:
        for text in plain:
            found = pattern.search(text)
            if found:
                return category, found.group(0)
    return None


def quota_refusal(*texts: Any) -> str | None:
    """The first platform quota-refusal wording in ``texts``, or ``None``."""
    found = platform_refusal(*texts)
    return found[1] if found and found[0] == "quota" else None


# ---------------------------------------------------------------------------
# the ledger: one JSON record per line, append-only, keyed by UTC day
# ---------------------------------------------------------------------------


class Ledger:
    """Append-only spend records. The day's total is the sum of ``billed_conservative``.

    A call that may spend is written down BEFORE it is sent (``call_start``, at its full
    reservation) and settled by its ``call`` record afterwards. A start with no settling record
    (the invocation was killed mid-call) counts at its full reservation.
    """

    SCHEMA = 3
    UNIT = "GPU-seconds: an UPPER BOUND from wall clock x size multiplier, NOT billed seconds"
    _SPENDING = ("call", "call_start")

    def __init__(self, path: str | Path):
        self.path = Path(path).expanduser()

    def records(self) -> list[dict]:
        if not self.path.exists():
            return []
        try:
            text = self.path.read_text()
        except OSError as exc:
            raise QuotaLedgerUnreadable(f"{self.path}: {exc}") from None
        records = []
        for number, line in enumerate(text.splitlines(), start=1):
            if not line.strip():
                continue
            try:
                record = json.loads(line)
                if not isinstance(record, dict) or "kind" not in record or "utc_day" not in record:
                    raise ValueError("not a ledger record")
                if record["kind"] in self._SPENDING:
                    float(record["billed_conservative"])
            except (ValueError, KeyError, TypeError) as exc:
                raise QuotaLedgerUnreadable(
                    f"{self.path} line {number} is not a ledger record ({exc}). Refusing to read it "
                    f"as zero spend: a damaged ledger is not evidence that nothing was spent. This "
                    f"version writes one JSON record per line; fix or move the file by hand."
                ) from None
            records.append(record)
        return records

    def _spending(self) -> list[dict]:
        """Settled calls, plus every start that was never settled."""
        records = self.records()
        settled = {r.get("call_id") for r in records if r["kind"] == "call" and r.get("call_id")}
        return [r for r in records if r["kind"] == "call"
                or r["kind"] == "call_start" and r.get("call_id") not in settled]

    def unsettled(self) -> list[dict]:
        return [r for r in self._spending() if r["kind"] == "call_start"]

    def spent(self, day: str) -> float:
        return sum(float(r["billed_conservative"]) for r in self._spending() if r["utc_day"] == day)

    def previous_day(self, day: str) -> tuple[str, float] | None:
        days: dict[str, float] = {}
        for record in self._spending():
            if record["utc_day"] < day:
                days[record["utc_day"]] = days.get(record["utc_day"], 0.0) + float(record["billed_conservative"])
        if not days:
            return None
        last = max(days)
        return last, days[last]

    def append(self, record: dict) -> None:
        line = redact(json.dumps({"schema": self.SCHEMA, **record}, sort_keys=True)) + "\n"
        self.path.parent.mkdir(parents=True, exist_ok=True)
        descriptor = os.open(self.path, os.O_WRONLY | os.O_APPEND | os.O_CREAT, 0o600)
        try:
            os.write(descriptor, line.encode())
            os.fsync(descriptor)
        finally:
            os.close(descriptor)


# ---------------------------------------------------------------------------
# the live contract
# ---------------------------------------------------------------------------


@dataclass
class Endpoint:
    name: str
    fn_index: int
    input_types: list[str]
    parameters: list[dict]
    returns: list[dict]


def find_endpoint(config: dict, info: dict, name: str) -> Endpoint | None:
    published = (info.get("named_endpoints") or {}).get(name)
    dependency = next(
        (d for d in config.get("dependencies", []) if "/" + str(d.get("api_name") or "") == name), None
    )
    if published is None or dependency is None:
        return None
    types = {c["id"]: str(c.get("type", "")).lower() for c in config.get("components", [])}
    return Endpoint(
        name=name,
        fn_index=int(dependency["id"]),
        input_types=[types.get(i, "?") for i in dependency.get("inputs", [])],
        parameters=list(published.get("parameters") or []),
        returns=list(published.get("returns") or []),
    )


def _expected_component(kind: str) -> str:
    return _COMPONENT_FOR_KIND.get(kind, kind)


def generate_contract_problems(parameters: list[dict], *, input_types: list[str] | None = None,
                               plan_modes=()) -> tuple[list[str], list[str]]:
    """``(problems, table)`` comparing ``/on_generate``'s published signature with the field spec."""
    expected = [("mode", None, None)] + [(f.key, f.kind, f.label) for f in api_fields()]
    problems: list[str] = []
    table = [f"  {'#':>2}  {'expected':<44} live"]
    for slot in range(max(len(expected), len(parameters))):
        want = expected[slot] if slot < len(expected) else None
        have = parameters[slot] if slot < len(parameters) else None
        live = "-" if have is None else f"{have.get('component')} {have.get('label')!r}"
        if want is None:
            problems.append(f"slot {slot}: the Space publishes {live} that the field spec does not have")
            table.append(f"  {slot:>2}  {'-':<44} {live}  ✗")
            continue
        key, kind, label = want
        shown = "mode (a choice)" if kind is None else f"{key} ({_expected_component(kind)} {label!r})"
        mark = "✓"
        if have is None:
            problems.append(f"slot {slot}: the field spec sends {key!r} and the Space has no parameter there")
            mark = "✗"
        elif kind is None:
            if str(have.get("component", "")).lower() not in ("radio", "dropdown"):
                problems.append(f"slot 0 should be the mode selector, the Space has {live}")
                mark = "✗"
        else:
            component = str(have.get("component", "")).lower()
            if component != _expected_component(kind):
                problems.append(f"slot {slot}: {key!r} is a {kind}; the Space has {live}")
                mark = "✗"
            elif label and have.get("label") and not str(have.get("label")).strip().startswith(str(label).strip()):
                # A prefix, because the UI may add detail to a label (stage 2 says what size it returns).
                # Labels are the only thing that tells two same-kind neighbours (width, height) apart.
                problems.append(f"slot {slot}: {key!r} is labelled {label!r} here; the Space has {live}")
                mark = "✗"
        table.append(f"  {slot:>2}  {shown:<44} {live}  {mark}")
    if parameters:
        choices = (parameters[0].get("type") or {}).get("enum")
        if choices:
            for mode_id in plan_modes:
                if mode_id not in choices:
                    problems.append(f"the Space does not offer mode {mode_id!r} (it offers {choices})")
    if input_types is not None:
        want_types = ["mode", *(f.kind for f in fields.FIELDS)]
        if len(input_types) != len(want_types):
            problems.append(
                f"{ENDPOINT} takes {len(input_types)} inputs on the Space; the field spec builds {len(want_types)}"
            )
        else:
            want_state = [i for i, kind in enumerate(want_types) if kind == "state"]
            have_state = [i for i, kind in enumerate(input_types) if kind == "state"]
            if want_state != have_state:
                problems.append(f"{ENDPOINT}'s State inputs sit at {have_state} on the Space, {want_state} here")
    return problems, table


@dataclass
class LoraRoute:
    endpoint: Endpoint
    source_slot: int | None
    upload_slot: int | None
    weight_slot: int | None
    weight_endpoint: Endpoint | None = None


_SOURCE_NAMES = {"repo_id", "repo", "source", "hub_id", "repo_or_url"}


def _lora_route(config: dict, info: dict) -> LoraRoute | None:
    endpoint = find_endpoint(config, info, LORA_ENDPOINT)
    if endpoint is None:
        return None
    source = upload = weight = None
    for slot, parameter in enumerate(endpoint.parameters):
        name = str(parameter.get("parameter_name") or "").lower()
        component = str(parameter.get("component", "")).lower()
        label = str(parameter.get("label") or "").lower()
        if weight is None and component in ("slider", "number") and ("weight" in name or "strength" in name
                                                                      or "strength" in label):
            weight = slot
        elif upload is None and (component in ("file", "uploadbutton") or "upload" in name):
            upload = slot
        elif source is None and component == "textbox" and (name in _SOURCE_NAMES or "repo" in label):
            source = slot
    weight_endpoint = find_endpoint(config, info, LORA_WEIGHT_ENDPOINT)
    if weight_endpoint is not None:
        names = [str(p.get("parameter_name") or "").lower() for p in weight_endpoint.parameters]
        if "index" not in names or "weight" not in names:
            weight_endpoint = None
    return LoraRoute(endpoint, source, upload, weight, weight_endpoint)


def lora_contract_problems(route: LoraRoute | None, calls: list[Call]) -> list[str]:
    problems: list[str] = []
    with_loras = [c for c in calls if c.loras]
    if not with_loras:
        return problems
    if route is None:
        return [f"{', '.join(c.label for c in with_loras)} stage LoRAs, and the Space publishes no {LORA_ENDPOINT}"]
    for call in with_loras:
        for lora in call.loras:
            if lora.local is not None and route.upload_slot is None:
                problems.append(f"{call.label}: {LORA_ENDPOINT} has no upload parameter for {lora.source}")
            if lora.local is None and route.source_slot is None:
                problems.append(f"{call.label}: {LORA_ENDPOINT} has no Hub repo id parameter for {lora.source}")
            if lora.needs_weight_route and route.weight_slot is None and route.weight_endpoint is None:
                names = [p.get("parameter_name") for p in route.endpoint.parameters]
                problems.append(
                    f"{call.label}: {lora.source} asks for strength {lora.weight:g}, and the Space exposes no "
                    f"public way to set an adapter's strength ({LORA_ENDPOINT} takes {names} and stages at "
                    f"{staged_lora_weight():g}; there is no {LORA_WEIGHT_ENDPOINT} with 'index' and 'weight'). "
                    f"Run the rest with --skip {call.label}, set its weight to {staged_lora_weight():g}, or "
                    f"publish that route."
                )
    return problems


def build_data(input_types: list[str], values: list) -> list:
    """Values for the non-State inputs in order, ``None`` in every State slot."""
    remaining = list(values)
    data = []
    for kind in input_types:
        if kind == "state":
            data.append(None)
        else:
            if not remaining:
                raise ContractMismatch(f"{len(values)} values for {input_types}")
            data.append(remaining.pop(0))
    if remaining:
        raise ContractMismatch(f"{len(values)} values for {input_types}")
    return data


def resolved_request(call: Call) -> dict:
    """Every non-state field by name, as it will be sent (media described, not uploaded)."""
    out: dict = {}
    width, height = call.render_size
    for spec in api_fields():
        if spec.kind in MEDIA_KINDS:
            item = call.media.get(spec.kind)
            out[spec.key] = None if item is None else item.describe()
        elif spec.key == "width":
            out[spec.key] = width
        elif spec.key == "height":
            out[spec.key] = height
        else:
            out[spec.key] = call.value(spec.key)
    return out


def generate_values(call: Call, uploaded: dict) -> list:
    values: list = [call.mode]
    request = resolved_request(call)
    for spec in api_fields():
        if spec.kind in MEDIA_KINDS:
            values.append(uploaded.get(spec.kind))
        elif spec.kind == "dataframe":
            values.append({"headers": list(spec.options.get("headers") or []), "data": request[spec.key] or [],
                           "metadata": None})
        else:
            values.append(request[spec.key])
    return values


# ---------------------------------------------------------------------------
# measurements (reported with thresholds; none of them stops a plan)
# ---------------------------------------------------------------------------


def _decode_video_frames(path, *, limit=512):
    import av

    frames = []
    with av.open(str(path)) as container:
        if not container.streams.video:
            return frames
        for frame in container.decode(video=0):
            frames.append(frame.to_ndarray(format="rgb24"))
            if len(frames) >= limit:
                break
    return frames


def _check(name, value, threshold, verdict, detail=""):
    return {"name": name, "value": value, "threshold": threshold, "verdict": verdict, "detail": detail}


def _verbatim(value: Any) -> Any:
    """Any value as JSON-able data, field for field, without interpreting it."""
    import dataclasses
    import enum

    if dataclasses.is_dataclass(value) and not isinstance(value, type):
        return {f.name: _verbatim(getattr(value, f.name)) for f in dataclasses.fields(value)}
    if isinstance(value, enum.Enum):
        return _verbatim(value.value)
    if isinstance(value, dict):
        return {str(k): _verbatim(v) for k, v in value.items()}
    if isinstance(value, (list, tuple, set, frozenset)):
        return [_verbatim(v) for v in value]
    if value is None or isinstance(value, (str, int, float, bool)):
        return value
    if hasattr(value, "item"):
        try:
            return value.item()
        except Exception:  # noqa: BLE001
            pass
    if hasattr(value, "__dict__"):
        return {k: _verbatim(v) for k, v in vars(value).items() if not k.startswith("_")}
    return str(value)


def picture_health(frames) -> Any:
    """The app's own picture check (``outputs.assess_picture``), recorded verbatim.

    The single call site. Nothing here judges what it returns: a fired condition is shown in the
    summary and never fails a call.
    """
    return _verbatim(outputs.assess_picture(frames))


def fired_conditions(health: Any) -> list[str]:
    conditions = health.get("conditions") if isinstance(health, dict) else None
    return [str(c) for c in conditions or [] if c]


def measure_video(path) -> dict:
    """Frame statistics (this harness's own thresholds) and the app's picture-health record."""
    import numpy as np

    try:
        frames = _decode_video_frames(path)
    except Exception as exc:  # noqa: BLE001
        return {"decoded_frames": 0, "checks": [_check("decodes", 0, "> 0 frames", "fail", f"{type(exc).__name__}: {exc}")]}
    if not frames:
        return {"decoded_frames": 0, "checks": [_check("decodes", 0, "> 0 frames", "fail", "no video frames")]}
    stack = np.stack(frames).astype("float32")
    deltas = [float(np.abs(stack[i + 1] - stack[i]).mean()) for i in range(len(frames) - 1)] or [0.0]
    spatial_std = float(np.median([frame.std() for frame in stack]))
    mean_delta = float(sum(deltas) / len(deltas))
    relative = None if spatial_std < MIN_PIXEL_STD else mean_delta / spatial_std
    distinct = len({frame.tobytes() for frame in frames})
    checks = [
        _check("flatness", round(spatial_std, 4), f">= {MIN_PIXEL_STD} median per-frame std",
               "pass" if spatial_std >= MIN_PIXEL_STD else "fail"),
        _check("distinct_frames", distinct, f">= {MIN_DISTINCT_FRAMES}",
               "pass" if distinct >= MIN_DISTINCT_FRAMES or len(frames) == 1 else "fail"),
        _check("freeze_floor", None if relative is None else round(relative, 6),
               f">= {MIN_RELATIVE_FRAME_DELTA} mean |delta| / spatial std",
               "n/a" if relative is None or len(frames) < 2 else
               ("pass" if relative >= MIN_RELATIVE_FRAME_DELTA else "fail")),
        _check("motion", round(mean_delta, 4), f">= {LOW_MEAN_FRAME_DELTA} mean |delta| (warn only)",
               "pass" if mean_delta >= LOW_MEAN_FRAME_DELTA else "warn",
               "a still-conditioned mode is expected to land low"),
    ]
    return {
        "decoded_frames": len(frames),
        "distinct_frames": distinct,
        "spatial_std": spatial_std,
        "stack_std": float(stack.std()),
        "mean_abs_delta": mean_delta,
        "min_abs_delta": min(deltas),
        "max_abs_delta": max(deltas),
        "relative_delta": relative,
        "checks": checks,
        "picture_health": picture_health(frames),
    }


#: Audio measurements. Reported, never judged: only digital silence fails a call.
SILENT_DBFS = -70.0
SILENCE_WINDOW_SECONDS = 0.02
SILENCE_WINDOW_DBFS = -60.0
CLIP_LEVEL = 0.999
SPECTRUM_FFT = 2048
SPECTRUM_MAX_FRAMES = 4096
ENERGY_BANDS_HZ = ((0, 250), (250, 2000), (2000, 6000), (6000, None))
#: An onset: the level of a short window rises at least ONSET_RISE_DB above the lowest level of
#: the windows just before it. Steady noise moves well under 2 dB window to window at this size;
#: speech, hits and chimes rise far more.
ONSET_WINDOW_SECONDS = 0.01
ONSET_LOOKBACK_SECONDS = 0.05
ONSET_RISE_DB = 8.0
ONSET_MIN_GAP_SECONDS = 0.1


def _dbfs(rms: float) -> float:
    import math

    return 20.0 * math.log10(rms + 1e-12)


def _decode_audio_track(path, index: int):
    """One audio stream as float32 ``[channels, samples]`` and its rate."""
    import av
    import numpy as np

    with av.open(str(path)) as container:
        stream = container.streams.audio[index]
        layout = getattr(getattr(stream, "layout", None), "name", None) or "stereo"
        resampler = av.AudioResampler(format="fltp", layout=layout, rate=int(stream.rate))
        blocks = []
        for frame in container.decode(stream):
            for out in resampler.resample(frame):
                blocks.append(out.to_ndarray())
        for out in resampler.resample(None):
            blocks.append(out.to_ndarray())
        rate = int(stream.rate)
    if not blocks:
        return np.zeros((1, 0), dtype="float32"), rate
    return np.concatenate(blocks, axis=-1).astype("float32"), rate


def _spectral(mono, rate: int) -> dict:
    import numpy as np

    if mono.size < SPECTRUM_FFT:
        mono = np.pad(mono, (0, SPECTRUM_FFT - mono.size))
    hop = SPECTRUM_FFT // 2
    count = 1 + (mono.size - SPECTRUM_FFT) // hop
    starts = hop * np.unique(np.linspace(0, count - 1, min(count, SPECTRUM_MAX_FRAMES)).astype(int))
    frames = mono[starts[:, None] + np.arange(SPECTRUM_FFT)[None, :]] * np.hanning(SPECTRUM_FFT)
    power = (np.abs(np.fft.rfft(frames, axis=1)) ** 2)[:, 1:]
    freqs = np.fft.rfftfreq(SPECTRUM_FFT, 1.0 / rate)[1:]
    mean = power.mean(axis=0)
    total = float(mean.sum())
    if total <= 0.0:
        return {"spectral_flatness": None, "spectral_flatness_frame_median": None,
                "spectral_centroid_hz": None, "band_energy_share": None, "band_level_dbfs": None}
    tiny = 1e-20

    def flatness(spectrum):
        return float(np.exp(np.mean(np.log(spectrum + tiny))) / (np.mean(spectrum) + tiny))

    live = power[power.sum(axis=1) > total * 1e-6]
    mean_square = float(np.mean(mono.astype("float64") ** 2))
    bands, levels = {}, {}
    for low, high in ENERGY_BANDS_HZ:
        mask = (freqs >= low) & (freqs < high) if high else (freqs >= low)
        name = f"{low}-{high or 'nyquist'}"
        share = float(mean[mask].sum()) / total
        bands[name] = round(share, 4)
        levels[name] = round(10.0 * np.log10(share * mean_square + 1e-12), 2)
    return {
        "spectral_flatness": round(flatness(mean), 4),
        "spectral_flatness_frame_median": round(float(np.median([flatness(f) for f in live])), 4) if len(live) else None,
        "spectral_centroid_hz": round(float((freqs * mean).sum() / total), 1),
        "band_energy_share": bands,
        "band_level_dbfs": levels,
    }


def count_onsets(mono, rate: int) -> int:
    """Rises of the short-window level of at least :data:`ONSET_RISE_DB` over the lowest level of
    the preceding :data:`ONSET_LOOKBACK_SECONDS`, out of silence or not, at least
    :data:`ONSET_MIN_GAP_SECONDS` apart."""
    import numpy as np

    window = max(1, int(round(ONSET_WINDOW_SECONDS * rate)))
    count = mono.size // window
    if count < 2:
        return 0
    frames = mono[: count * window].reshape(count, window).astype("float64")
    level = 10.0 * np.log10(np.mean(frames**2, axis=1) + 1e-12)
    back = max(1, int(round(ONSET_LOOKBACK_SECONDS / ONSET_WINDOW_SECONDS)))
    gap = max(1, int(round(ONSET_MIN_GAP_SECONDS / ONSET_WINDOW_SECONDS)))
    onsets, last = 0, -gap
    for index in range(1, count):
        floor = level[max(0, index - back) : index].min()
        if level[index] >= SILENCE_WINDOW_DBFS and level[index] - floor >= ONSET_RISE_DB and index - last >= gap:
            onsets += 1
            last = index
    return onsets


def measure_audio(path) -> dict:
    """Every audio stream of the file, as measurements. No verdicts."""
    import av
    import numpy as np

    try:
        with av.open(str(path)) as container:
            count = len(container.streams.audio)
    except Exception as exc:  # noqa: BLE001
        return {"tracks": [], "error": f"{type(exc).__name__}: {exc}"}
    tracks = []
    for index in range(count):
        try:
            samples, rate = _decode_audio_track(path, index)
        except Exception as exc:  # noqa: BLE001
            tracks.append({"index": index, "error": f"{type(exc).__name__}: {exc}"})
            continue
        channels, length = samples.shape
        if length == 0:
            tracks.append({"index": index, "sample_rate": rate, "channels": channels, "samples": 0,
                           "level_dbfs": _dbfs(0.0)})
            continue
        rms = float(np.sqrt(np.mean(samples.astype("float64") ** 2)))
        peak = float(np.abs(samples).max())
        window = max(1, int(round(SILENCE_WINDOW_SECONDS * rate)))
        usable = (length // window) * window
        if usable:
            windows = samples[:, :usable].reshape(channels, -1, window)
            window_rms = np.sqrt(np.mean(windows.astype("float64") ** 2, axis=(0, 2)))
            silence = float(np.mean(20.0 * np.log10(window_rms + 1e-12) < SILENCE_WINDOW_DBFS))
        else:
            silence = None
        clipped = int(np.count_nonzero(np.abs(samples) >= CLIP_LEVEL))
        mono = samples.mean(axis=0)
        onsets = count_onsets(mono, rate)
        tracks.append({
            "index": index,
            "sample_rate": rate,
            "channels": channels,
            "samples": length,
            "seconds": round(length / rate, 4),
            "level_dbfs": round(_dbfs(rms), 2),
            "rms": rms,
            "peak": peak,
            "peak_dbfs": round(_dbfs(peak), 2),
            "clipped_samples": clipped,
            "clipped_ratio": clipped / float(samples.size),
            "silence_ratio": silence,
            "onsets": onsets,
            "onsets_per_second": round(onsets / (length / rate), 3),
            **_spectral(mono, rate),
        })
    return {
        "tracks": tracks,
        "definitions": {
            "level_dbfs": "RMS over every sample of every channel",
            "clipped_samples": f"samples with |x| >= {CLIP_LEVEL} of full scale",
            "silence_ratio": f"share of {SILENCE_WINDOW_SECONDS * 1000:g} ms windows below {SILENCE_WINDOW_DBFS:g} dBFS",
            "onsets": f"rises of the {ONSET_WINDOW_SECONDS * 1000:g} ms level by >= {ONSET_RISE_DB:g} dB over the lowest "
                      f"level of the preceding {ONSET_LOOKBACK_SECONDS * 1000:g} ms, >= "
                      f"{ONSET_MIN_GAP_SECONDS * 1000:g} ms apart (steady noise has none; speech and hits many)",
            "spectral_flatness": "geometric / arithmetic mean of the mean power spectrum of the channels' mean "
                                 "(about 1 for white noise, far below 1 for speech or music)",
            "spectral_flatness_frame_median": f"the same per {SPECTRUM_FFT}-sample frame, median over frames",
            "spectral_centroid_hz": "power-weighted mean frequency",
            "band_energy_share": "share of spectral energy per band, Hz",
            "band_level_dbfs": "each band's energy as a level, dBFS (the bands add up to the channels' mean)",
        },
        "note": "Measurements only: nothing here is a verdict. Only digital silence fails a call.",
    }


def _audio_line(measured: dict) -> str:
    parts = []
    for track in measured.get("tracks", []):
        if "error" in track:
            parts.append(f"track {track['index']}: {track['error']}")
            continue
        flat = track.get("spectral_flatness")
        centroid = track.get("spectral_centroid_hz")
        silence = track.get("silence_ratio")
        bands = track.get("band_energy_share") or {}
        parts.append(
            f"{track['level_dbfs']:.1f} dBFS · peak {track.get('peak_dbfs', 0):.1f} · clipped "
            f"{track.get('clipped_samples', 0)} · silence {0 if silence is None else 100 * silence:.0f}% · flatness "
            f"{'n/a' if flat is None else f'{flat:.3f}'} · centroid "
            f"{'n/a' if centroid is None else f'{centroid:.0f} Hz'} · onsets {track.get('onsets', 'n/a')}"
            + (" · bands " + " ".join(f"{name} {100 * share:.0f}%" for name, share in bands.items()) if bands else "")
        )
    return " | ".join(parts)


def _count_frames(path) -> int | None:
    try:
        return len(_decode_video_frames(path, limit=100_000))
    except Exception:  # noqa: BLE001
        return None


def returned_files(spec) -> dict[str, tuple[str, ...]]:
    """The file the app returns in each output slot for this mode, by the tracks it carries.

    From the registry's ``exports``, as the app returns them: a slot holds a file exactly when
    the mode exports that track (``ui.handlers``). The video file carries every exported track;
    beside it, a mode that also exports audio returns the same sound as a file of its own,
    audio only (``outputs.write_output``). An audio-only mode's one file is its audio.
    """
    exported = [slot for slot in OUTPUT_SLOTS if slot in spec.exports]
    return {slot: tuple(exported) if index == 0 else (slot,) for index, slot in enumerate(exported)}


def _sound_on_its_own(path, *, sampling_rate: int | None) -> dict:
    """The sound beside the video file, judged as audio only: an audio stream and no video,
    stereo, at the expected rate. A file that cannot be probed is never a pass."""
    expected = {"video": False, "audio": True, "audio_channels": verify_streams.EXPECTED_AUDIO_CHANNELS}
    if sampling_rate is not None:
        expected["sampling_rate"] = sampling_rate
    actual = verify_streams.probe_streams(path)
    if actual is None:
        return {"probed": False, "ok": False, "expected": expected, "actual": {}, "problems": [
            "it could not be opened as a media container, so nothing about the sound on its own has been "
            "established. This is NOT a pass"]}
    problems = []
    if not actual["audio"]:
        problems.append("the sound on its own has NO audio stream")
    if actual["video"]:
        problems.append("the sound on its own has a video stream: the audio output must be the sound by itself")
    channels = actual.get("audio_channels")
    if actual["audio"] and channels is not None and channels != verify_streams.EXPECTED_AUDIO_CHANNELS:
        problems.append(f"audio has {channels} channel(s), expected {verify_streams.EXPECTED_AUDIO_CHANNELS} — "
                        f"something downmixed the vocoder's stereo output")
    if actual["audio"] and sampling_rate is not None and actual.get("sampling_rate") not in (None, sampling_rate):
        problems.append(f"audio sample rate is {actual.get('sampling_rate')}, expected {sampling_rate} — the file "
                        f"disagrees with the vocoder")
    return {"probed": True, "ok": not problems, "expected": expected, "actual": actual, "problems": problems}


def _described(slot: str, tracks: tuple[str, ...], *, beside: bool) -> str:
    if beside:
        return "its sound on its own beside the video, as an audio-only file"
    return f"a {slot} file with " + " and ".join(tracks)


def _seconds(measured: dict | None) -> float | None:
    """The first audio track's length, from its sample count and rate."""
    tracks = (measured or {}).get("tracks") or [{}]
    samples, rate = tracks[0].get("samples"), tracks[0].get("sample_rate")
    return samples / rate if samples is not None and rate else None


def verify_call(call: Call, artifacts: dict[str, Path], *, sampling_rate: int | None) -> dict:
    """What the mode promises, judged over the set of files the call returned, plus measurements.

    Each file is judged by what its slot holds (:func:`returned_files`): the video file against
    every track the mode exports (``verify_streams``, with the geometry); the sound beside it as
    audio only, and its length against the video's own sound. A promised file that did not come
    back, and a file the mode never returns, are mismatches.
    """
    problems: list[str] = []
    streams = []
    rate = call.expect.get("sample_rate", sampling_rate)
    geometry = call.expected_geometry()
    files = returned_files(call.spec)
    primary = next(iter(files))
    for slot in dict.fromkeys([*OUTPUT_SLOTS, *artifacts]):
        path, tracks = artifacts.get(slot), files.get(slot)
        if path is None:
            if tracks:
                problems.append(f"no {slot} file came back: {call.mode} returns "
                                f"{_described(slot, tracks, beside=slot != primary)}")
            continue
        if tracks is None:
            actual = verify_streams.probe_streams(path)
            entry = {"judged_as": f"a file {call.mode} never returns", "probed": actual is not None, "ok": False,
                     "expected": None, "actual": actual or {}, "problems": [
                         f"{call.mode} returns no {slot} file (it exports {' and '.join(call.exports)}), and this "
                         f"one came back as its {slot} output: a file the mode never returns is not a pass"]}
        elif slot == primary:
            verdict = verify_streams.verify(
                call.mode,
                path,
                frame_rate=call.expect.get("frame_rate"),
                sampling_rate=rate if "audio" in tracks else None,
                geometry=geometry if "video" in tracks else None,
            )
            entry = {"judged_as": f"every track {call.mode} exports: {', '.join(tracks)}", "probed": verdict.probed,
                     "ok": verdict.ok, "expected": verdict.expected, "actual": verdict.actual,
                     "problems": verdict.problems}
        elif path == artifacts.get(primary):
            actual = verify_streams.probe_streams(path)
            entry = {"judged_as": "the sound on its own: audio only", "probed": actual is not None, "ok": False,
                     "expected": None, "actual": actual or {}, "problems": [
                         f"the {primary} file came back as the {slot} output too: {call.mode} returns its sound "
                         f"there as a file of its own, audio only"]}
        else:
            entry = {"judged_as": "the sound on its own: audio only", **_sound_on_its_own(path, sampling_rate=rate)}
        streams.append({"slot": slot, "path": str(path), **entry})
        problems += [f"{Path(path).name}: {p}" for p in entry["problems"]]
    if primary in artifacts:
        probe = next(s["actual"] for s in streams if s["slot"] == primary) or {}
        for track in ("video", "audio"):
            wanted = call.expect.get(f"has_{track}")
            if wanted is not None and bool(probe.get(track)) != bool(wanted):
                problems.append(f"expected has_{track}={wanted}, the {primary} file has {track}={bool(probe.get(track))}")
    frames = None
    if "video" in artifacts:
        frames = _count_frames(artifacts["video"])
        wanted_frames = call.expected_frames()
        if wanted_frames is not None and frames != wanted_frames:
            problems.append(f"decoded {frames} frames, expected {wanted_frames}")
    content: dict = {}
    if "video" in artifacts:
        content["video"] = measure_video(artifacts["video"])
    if "audio" in artifacts:
        content["audio"] = measure_audio(artifacts["audio"])
    if primary == "video" and "audio" in files[primary] and primary in artifacts \
            and artifacts[primary] != artifacts.get("audio"):
        # The video file's own sound, which the sound on its own must match.
        content["audio_in_video"] = measure_audio(artifacts[primary])
    for key, slot in (("audio", "audio"), ("audio_in_video", primary)):
        for track in (content.get(key) or {}).get("tracks", []):
            level = track.get("level_dbfs")
            if level is not None and level < SILENT_DBFS:
                problems.append(f"{Path(artifacts[slot]).name}: audio track {track['index']} is digital silence: "
                                f"{level:.1f} dBFS < {SILENT_DBFS:g}")
    lengths = None
    own, inside = _seconds(content.get("audio")), _seconds(content.get("audio_in_video"))
    if own is not None and inside is not None:
        tolerance = constraints.DEFAULT_LENGTH_TOLERANCE_SECONDS
        lengths = {"sound_on_its_own_seconds": round(own, 4), "sound_in_video_seconds": round(inside, 4),
                   "difference_seconds": round(abs(own - inside), 4), "tolerance_seconds": tolerance}
        if abs(own - inside) > tolerance:
            problems.append(
                f"{Path(artifacts['audio']).name} is {own:.3f} s and the sound in {Path(artifacts[primary]).name} "
                f"{inside:.3f} s: the sound on its own does not match the video's sound (the app allows "
                f"{tolerance:g} s between a run's audio and video)"
            )
    return {
        "ok": not problems,
        "problems": problems,
        "expected": {"geometry": list(geometry) if geometry else None, "frames": call.expected_frames(),
                     "sample_rate": rate if "audio" in call.exports else None,
                     "files": {slot: list(tracks) for slot, tracks in files.items()},
                     **{k: v for k, v in call.expect.items() if k.startswith("has_")}},
        "decoded_frames": frames,
        "streams": streams,
        "sound_lengths": lengths,
        "content": content,
        "content_note": (
            "Content figures are measurements. The frame checks carry this harness's thresholds, the "
            "picture-health record is the app's own check verbatim, and audio has no verdict. None of "
            "them stops a plan; no artifact is a pass until a frame has been looked at."
        ),
    }


def _flags(verify: dict) -> list[str]:
    flagged = []
    content = verify.get("content") or {}
    for check in (content.get("video") or {}).get("checks", []):
        if check["verdict"] in ("fail", "warn"):
            flagged.append(f"video.{check['name']}={check['verdict']}")
    for condition in fired_conditions((content.get("video") or {}).get("picture_health")):
        flagged.append(f"picture: {condition}")
    return flagged


# ---------------------------------------------------------------------------
# chained inputs
# ---------------------------------------------------------------------------


def extract_image(video: Path, dest: Path, frame) -> int:
    from PIL import Image

    frames = _decode_video_frames(video, limit=100_000)
    if not frames:
        raise RuntimeError(f"{video} has no frames to take an image from")
    index = {"middle": len(frames) // 2, "last": len(frames) - 1}.get(frame, frame)
    if not isinstance(index, int) or not 0 <= index < len(frames):
        raise RuntimeError(f"{video} has {len(frames)} frames; frame {frame!r} does not exist")
    dest.parent.mkdir(parents=True, exist_ok=True)
    Image.fromarray(frames[index]).save(str(dest))
    return index


def extract_audio(source: Path, dest: Path) -> Path:
    import av

    dest.parent.mkdir(parents=True, exist_ok=True)
    with av.open(str(source)) as probe:
        if not probe.streams.audio:
            raise RuntimeError(f"{source} has no audio stream")
        stream = probe.streams.audio[0]
        already = probe.format.name in ("wav", "wave") and stream.codec_context.name.startswith("pcm")
    if already:
        shutil.copyfile(source, dest)
        return dest
    with av.open(str(source)) as container, av.open(str(dest), "w", format="wav") as out:
        stream = container.streams.audio[0]
        layout = getattr(getattr(stream, "layout", None), "name", None) or "stereo"
        out_stream = out.add_stream("pcm_s16le", rate=int(stream.rate), layout=layout)
        for frame in container.decode(stream):
            frame.pts = None
            for packet in out_stream.encode(frame):
                out.mux(packet)
        for packet in out_stream.encode():
            out.mux(packet)
    return dest


def _sha256(path: Path) -> str:
    digest = hashlib.sha256()
    with Path(path).open("rb") as handle:
        for chunk in iter(lambda: handle.read(1 << 20), b""):
            digest.update(chunk)
    return digest.hexdigest()


# ---------------------------------------------------------------------------
# one live call
# ---------------------------------------------------------------------------


class Evidence:
    """``--out/<label>/``: every file written here is redacted."""

    def __init__(self, directory: Path):
        self.dir = directory
        self.dir.mkdir(parents=True, exist_ok=True)
        self._events = (self.dir / "events.jsonl").open("a", encoding="utf-8")

    def event(self, record: dict) -> None:
        self._events.write(redact(json.dumps(record, sort_keys=True, default=str)) + "\n")
        self._events.flush()

    def write(self, name: str, payload: Any) -> Path:
        path = self.dir / name
        path.write_text(_redacted_json(payload))
        return path

    def close(self) -> None:
        self._events.close()


@dataclass
class Outcome:
    label: str
    status: str  # ok | mismatch | failed | timeout | quota | skipped
    detail: str = ""
    category: str | None = None
    billed_conservative: float = 0.0
    reserved: int = 0
    artifacts: dict = field(default_factory=dict)
    flags: list = field(default_factory=list)
    audio: str = ""

    @property
    def failed(self) -> bool:
        return self.status in ("mismatch", "failed", "timeout")


@dataclass
class Session:
    """Everything one live invocation shares."""

    app: SpaceApp
    generate: Endpoint
    lora: LoraRoute | None
    ledger: Ledger
    out: Path
    log: Any
    invocation: str
    ceiling: float
    floor_seconds: int | None
    multiplier: float
    call_timeout: float
    read_timeout: float
    sampling_rate: int
    space_sha: str | None
    produced: dict = field(default_factory=dict)


class CeilingReached(RuntimeError):
    pass


def _find_record(data: list, status: str) -> dict | None:
    """A structured run record, if the API carries one: an output beyond the three published
    ones, or JSON embedded in the status. The report's prose is never read for facts."""
    for extra in data[3:]:
        if isinstance(extra, dict):
            return extra
    found = re.search(r'<script[^>]*type="application/json"[^>]*>(.*?)</script>', status or "", re.S)
    if found is None:
        return None
    try:
        record = json.loads(html.unescape(found.group(1)))
    except ValueError:
        return None
    return record if isinstance(record, dict) else None


def _record_value(record: dict | None, *names) -> Any:
    if not record:
        return None
    for name in names:
        value = record.get(name)
        if value is None and isinstance(record.get("failure"), dict):
            value = record["failure"].get(name)
        if value is not None:
            return value
    return None


class _Reported(Exception):
    """What reached the API, shaped for ``errors.categorise``: the title (when one came) and the
    message. The exception's own type does not cross the API."""

    def __init__(self, title: str, message: str):
        super().__init__(message)
        self.title = title
        self.message = message


def classify(record: dict | None, *, title: Any = None, error: Any = None, status: str = "") -> dict:
    """A failed call's category, as it came: the run's structured record's when the API carries
    one, else the platform's own refusal wording, else ``errors.categorise`` (the app's
    classifier) on the text that reached the API. An unknown string is kept as it is."""
    value = _record_value(record, "category")
    if value:
        return {"category": str(value), "source": "the run's structured record"}
    found = platform_refusal(title, error, status)
    if found:
        return {"category": found[0], "source": "the platform's refusal wording", "platform_words": found[1]}
    from ltx25_studio import errors

    words = " ".join(str(text) for text in (error, _plain(status)) if text)
    failure = errors.categorise(_Reported(str(title or ""), words))
    return {"category": str(failure.category), "gpu_used": bool(failure.gpu_used),
            "source": "errors.categorise on the text that reached the API"}


def _refusal_category(exc: BaseException) -> str:
    """The category of a refusal raised here, before anything was sent: the app's own."""
    if isinstance(exc, PlanError):
        return "input"
    from ltx25_studio import errors

    return str(errors.categorise(exc).category)


def _plain(markdown: str) -> str:
    text = re.sub(r"<script.*?</script>", "", markdown or "", flags=re.S)
    text = re.sub(r"<[^>]+>", " ", text)
    return re.sub(r"[ \t]+", " ", html.unescape(text)).strip()


def _file_path(value) -> str | None:
    if isinstance(value, str):
        return value or None
    if isinstance(value, dict):
        if isinstance(value.get("path"), str):
            return value["path"]
        for key in ("video", "audio", "file"):
            inner = _file_path(value.get(key))
            if inner:
                return inner
    return None


def _run_events(ctx: Session, evidence: Evidence, endpoint: Endpoint, data: list, session_hash: str,
                started: float, deadline: float, timing: dict, key: str) -> dict:
    """Join one event and read its stream to completion. Records every message."""
    evidence.event({"t": round(_monotonic() - started, 4), "endpoint": endpoint.name, "client": "join"})
    event_id = ctx.app.join(endpoint.fn_index, data, session_hash)
    timing[f"{key}_event_id_t"] = round(_monotonic() - started, 4)
    evidence.event({"t": timing[f"{key}_event_id_t"], "endpoint": endpoint.name, "client": "event_id",
                    "event_id": event_id})
    try:
        with ctx.app.stream(session_hash, read_timeout=min(ctx.read_timeout, ctx.call_timeout)) as response:
            for message in iter_sse(response):
                now = _monotonic()
                t = round(now - started, 4)
                evidence.event({"t": t, "utc": _utcnow().isoformat(), "endpoint": endpoint.name, **message})
                kind = message.get("msg") if isinstance(message, dict) else None
                if kind == "process_starts":
                    timing.setdefault(f"{key}_process_starts_t", t)
                elif kind == "progress":
                    timing.setdefault(f"{key}_first_progress_t", t)
                    if key == "generate":
                        for unit in message.get("progress_data") or []:
                            ctx.log(f"    progress  {unit.get('desc') or ''} {unit.get('index')}/{unit.get('length')}"
                                    f" {unit.get('unit') or ''}".rstrip())
                elif kind == "log" and key == "generate":
                    ctx.log(f"    space     {message.get('title') or ''}: {message.get('log')}")
                elif kind in ("process_completed", "unexpected_error") and message.get("event_id") in (event_id, None):
                    timing[f"{key}_completed_t"] = t
                    return message
                elif kind == "close_stream":
                    break
                if now > deadline:
                    raise CallTimeout(f"no completion within {ctx.call_timeout:g} s")
    except TimeoutError:
        raise CallTimeout(f"the event stream went silent for {ctx.read_timeout:g} s") from None
    except TransportError as exc:
        if "timed out" in str(exc):
            raise CallTimeout(f"the event stream went silent for {ctx.read_timeout:g} s") from None
        raise
    raise TransportError("the event stream closed before the call completed")


def _validate_loras(ctx: Session, call: Call, evidence: Evidence, session_hash: str, started: float,
                    timing: dict) -> list[dict]:
    route = ctx.lora
    verdicts = []
    for index, lora in enumerate(call.loras):
        values: list = [None] * len(route.endpoint.parameters)
        if lora.local is not None:
            values[route.upload_slot] = ctx.app.upload(lora.local)
            if route.source_slot is not None:
                values[route.source_slot] = ""
        else:
            values[route.source_slot] = lora.source
        if route.weight_slot is not None:
            values[route.weight_slot] = lora.weight
        data = build_data(route.endpoint.input_types, values)
        deadline = _monotonic() + ctx.call_timeout
        message = _run_events(ctx, evidence, route.endpoint, data, session_hash, started, deadline, timing,
                              f"lora{index}")
        output = message.get("output") or {}
        texts = [str(v) for v in output.get("data") or [] if isinstance(v, str)]
        verdict = texts[0] if texts else str(output.get("error") or message.get("message") or "")
        staged_list = texts[1:]
        name = Path(lora.label).name
        staged = any(name in text for text in staged_list) if staged_list else None
        if message.get("success") is False or message.get("msg") == "unexpected_error":
            staged = False
        entry = {**lora.describe(), "verdict": verdict, "staged_list": staged_list, "staged": staged}
        if staged and lora.needs_weight_route and route.weight_slot is None and route.weight_endpoint is not None:
            weights = {"index": index, "weight": lora.weight}
            names = [str(p.get("parameter_name") or "").lower() for p in route.weight_endpoint.parameters]
            data = build_data(route.weight_endpoint.input_types, [weights.get(n) for n in names])
            done = _run_events(ctx, evidence, route.weight_endpoint, data, session_hash, started,
                               _monotonic() + ctx.call_timeout, timing, f"lora{index}_weight")
            entry["weight_set"] = done.get("success", True) is not False
        verdicts.append(entry)
        ctx.log(f"    lora      {lora.label}: {'staged' if staged else 'NOT staged' if staged is False else 'unconfirmed'}"
                f" · {_plain(verdict)[:160]}")
        if staged is False:
            break
    return verdicts


def _resolve_media(ctx: Session, call: Call, evidence: Evidence) -> dict[str, dict]:
    resolved: dict[str, dict] = {}
    for kind, item in call.media.items():
        if not item.chained:
            resolved[kind] = {"local": item.path, "chained": False, "sha256": _sha256(item.path)}
            continue
        artifacts = ctx.produced.get(item.from_call)
        track = "audio" if kind == "audio" else "video"
        source = (artifacts or {}).get(track)
        if source is None:
            raise RuntimeError(f"{kind} is chained from {item.from_call!r}, which produced no {track}")
        inputs = evidence.dir / "inputs"
        if kind == "image":
            dest = inputs / "image.png"
            index = extract_image(Path(source), dest, item.frame)
        elif kind == "video":
            dest = inputs / f"video{Path(source).suffix or '.mp4'}"
            dest.parent.mkdir(parents=True, exist_ok=True)
            shutil.copyfile(source, dest)
            index = None
        else:
            dest = extract_audio(Path(source), inputs / "audio.wav")
            index = None
        resolved[kind] = {"local": dest, "chained": True, "from_call": item.from_call, "source_artifact": str(source),
                          "frame_index": index, "sha256": _sha256(dest)}
    return resolved


def run_call(ctx: Session, call: Call) -> Outcome:
    day = utc_day()
    reserved = reservation_seconds(call, floor_seconds=ctx.floor_seconds)
    worst = reserved * ctx.multiplier
    spent = ctx.ledger.spent(day)
    if spent + worst > ctx.ceiling:
        rollover = next_utc_midnight()
        left = max(0.0, ctx.ceiling - spent)
        raise CeilingReached(
            f"stopped before {call.label}: today (UTC {day}) has <= {spent:.1f} GPU-s on the ledger, and "
            f"this call reserves {reserved} s x {ctx.multiplier:g} = {worst:.1f}, which would pass the "
            f"{ctx.ceiling:.0f} GPU-s ceiling. {left:.1f} GPU-s are left today; the UTC day rolls over at "
            f"{rollover.isoformat().replace('+00:00', 'Z')} (in {_duration(rollover - _utcnow())}). Nothing was sent."
        )

    evidence = Evidence(ctx.out / call.label)
    started = _monotonic()
    timing: dict = {"reserved_seconds": reserved, "multiplier": ctx.multiplier, "worst_case_billed": worst}
    request = {
        "label": call.label,
        "mode": call.mode,
        "note": call.note,
        "fields": resolved_request(call),
        "loras": [lora.describe() for lora in call.loras],
        "expect": call.expect,
        "reservation_seconds": reserved,
        "space_sha": ctx.space_sha,
    }
    evidence.write("request.json", request)
    ctx.log(f"\n>>> {call.label}  {call.mode}  reserve {reserved} s (<= {worst:.0f} GPU-s worst case)")
    session_hash = uuid.uuid4().hex
    outcome = Outcome(label=call.label, status="failed", reserved=reserved)
    message: dict | None = None
    call_id = uuid.uuid4().hex[:16]
    ledger_record = {"kind": "call", "call_id": call_id, "utc_day": day, "invocation": ctx.invocation,
                     "label": call.label, "mode": call.mode, "reserved_seconds": reserved,
                     "multiplier": ctx.multiplier, "billed_conservative": 0.0, "billed_worker": None,
                     "space_sha": ctx.space_sha}
    sent = False

    def settle(status: str, billed: float, **extra) -> None:
        """The call's one closing ledger record."""
        outcome.billed_conservative = billed
        ctx.ledger.append({**ledger_record, "at": _utcnow().isoformat(), "outcome": status,
                           "billed_conservative": round(billed, 3), **extra})

    def unsent(detail: str, category: str, **extra) -> Outcome:
        """Refused here, before the run was sent: nothing billed."""
        outcome.detail, outcome.category = detail, category
        settle("failed", 0.0, error=detail, category=category)
        evidence.write("result.json", {"outcome": "failed", "sent": False, "detail": detail,
                                       "category": category, **extra})
        return outcome

    def no_completion() -> float:
        """No completion was seen: the window so far, never less than the reservation."""
        start_t = timing.get("generate_process_starts_t", timing.get("generate_event_id_t", 0.0))
        return max(_monotonic() - started - start_t, reserved) * ctx.multiplier

    try:
        try:
            media = _resolve_media(ctx, call, evidence)
        except Exception as exc:  # noqa: BLE001
            request["media"] = {"error": redact(str(exc))}
            evidence.write("request.json", request)
            return unsent(f"input could not be prepared: {redact(exc)}", "input")
        request["media"] = {k: {kk: str(vv) if isinstance(vv, Path) else vv for kk, vv in v.items()}
                            for k, v in media.items()}
        try:
            # The app's own gate on the real files, as the Space runs it: what a dry run could
            # only defer for a chained input is checked here, before anything is sent.
            call = gated(call, {k: v["local"] for k, v in media.items()})
        except Exception as exc:  # noqa: BLE001 -- refused before dispatch: nothing billed
            evidence.write("request.json", request)
            return unsent(f"the app refuses this call before reserving a GPU: {redact(exc)}",
                          _refusal_category(exc))
        request.update(app_notes=list(call.app_notes), app_frames=call.app_frames, app_level=call.app_level,
                       app_changes=[{"field": k, "sent": s, "runs": r} for k, s, r in call.app_changes])
        evidence.write("request.json", request)

        if call.loras:
            verdicts = _validate_loras(ctx, call, evidence, session_hash, started, timing)
            request["loras"] = verdicts
            evidence.write("request.json", request)
            rejected = [v for v in verdicts if v["staged"] is False]
            if rejected:
                return unsent(f"LoRA not staged: {_plain(rejected[0]['verdict'])[:400]}", "adapter", loras=verdicts)

        uploaded = {kind: ctx.app.upload(item["local"]) for kind, item in media.items()}
        timing["uploaded_t"] = round(_monotonic() - started, 4)
        data = build_data(ctx.generate.input_types, generate_values(call, uploaded))
        deadline = _monotonic() + ctx.call_timeout
        # Written down at its full reservation BEFORE it is sent, settled below: a call this
        # process never comes back from still counts against the day.
        ctx.ledger.append({**ledger_record, "kind": "call_start", "at": _utcnow().isoformat(),
                           "billed_conservative": round(worst, 3)})
        sent = True
        evidence.write("result.json", {"outcome": "sent", "sent": True,
                                       "note": "sent to the Space; replaced when the call completes"})
        try:
            message = _run_events(ctx, evidence, ctx.generate, data, session_hash, started, deadline, timing,
                                  "generate")
        except (CallTimeout, TransportError) as exc:
            billed = no_completion()
            outcome.status = "timeout" if isinstance(exc, CallTimeout) else "failed"
            outcome.detail = redact(str(exc))
            outcome.category = "timeout" if isinstance(exc, CallTimeout) else "transport"
            timing.update(billed_conservative=round(billed, 3),
                          billed_rule="max(window, reservation) x multiplier: no completion")
            settle(outcome.status, billed, error=outcome.detail, category=outcome.category)
            evidence.write("timing.json", timing)
            evidence.write("result.json", {"outcome": outcome.status, "sent": True, "detail": outcome.detail,
                                           "category": outcome.category})
            return outcome

        start_t = timing.get("generate_process_starts_t", timing.get("generate_event_id_t", 0.0))
        window = max(0.0, timing["generate_completed_t"] - start_t)
        output = message.get("output") or {}
        data_out = list(output.get("data") or [])
        status = str(data_out[2]) if len(data_out) > 2 and data_out[2] is not None else ""
        record = _find_record(data_out, status)
        # The worker's own GPU time only from a structured record; the report's prose is not read.
        worker = _record_value(record, "elapsed_seconds")
        billed_worker = None if worker is None else float(worker) * ctx.multiplier
        error = None
        if message.get("success") is False or message.get("msg") == "unexpected_error":
            error = output.get("error") or message.get("message") or (
                "the Space raised an error and did not say what (its show_error is off)"
            )
        missing = [slot for index, slot in enumerate(OUTPUT_SLOTS)
                   if slot in returned_files(call.spec) and not (index < len(data_out) and _file_path(data_out[index]))]
        failure = classify(record, title=output.get("title"), error=error, status=status) if error or missing \
            else None
        category = failure["category"] if failure else None
        billed = 0.0 if category in UNBILLED_CATEGORIES else window * ctx.multiplier
        timing.update(window_seconds=round(window, 4), billed_conservative=round(billed, 3),
                      billed_worker=None if billed_worker is None else round(billed_worker, 3),
                      worker_elapsed_seconds=worker,
                      billed_rule=f"zero: the platform refused ({category}) before a GPU was used"
                      if category in UNBILLED_CATEGORIES else "(completed - process_starts) x multiplier")
        outcome.category = category
        words = (failure or {}).get("platform_words")
        closing = ("quota_refusal" if category == "quota" else "platform_refusal" if category in UNBILLED_CATEGORIES
                   else "failed" if failure else "completed")
        settle(closing, billed,
               billed_worker=None if billed_worker is None else round(billed_worker, 3),
               error=redact(words or error or (_plain(status)[:600] if failure else "")) or None,
               category=category)
        evidence.write("timing.json", timing)
        result = {"success": message.get("success"), "status_markdown": status, "status_text": _plain(status),
                  "error": error, "title": output.get("title"), "record": record, "category": category,
                  "classified": failure, "outputs": data_out}

        if category == "quota":
            outcome.status = "quota"
            outcome.detail = redact(error or _plain(status) or words)
            evidence.write("result.json", {**result, "outcome": "quota"})
            return outcome

        artifacts: dict[str, Path] = {}
        downloaded: dict[str, Path] = {}
        for index, track in enumerate(OUTPUT_SLOTS):
            server_path = _file_path(data_out[index]) if index < len(data_out) else None
            if not server_path:
                continue
            if server_path not in downloaded:
                downloaded[server_path] = ctx.app.download(server_path, evidence.dir / f"{track}_{Path(server_path).name}")
            artifacts[track] = downloaded[server_path]
        timing["downloaded_t"] = round(_monotonic() - started, 4)
        evidence.write("timing.json", timing)
        result["artifacts"] = {k: {"path": str(v), "bytes": v.stat().st_size, "sha256": _sha256(v)} for k, v in artifacts.items()}
        if failure:
            tracks = " and ".join(missing)
            outcome.detail = redact(error or f"no {tracks} file; the Space said: {_plain(status)[:600]}")
            outcome.status = "failed"
            evidence.write("result.json", {**result, "outcome": "failed", "detail": outcome.detail})
            return outcome

        verify = verify_call(call, artifacts, sampling_rate=ctx.sampling_rate)
        evidence.write("verify.json", verify)
        outcome.artifacts = artifacts
        outcome.flags = _flags(verify)
        outcome.audio = _audio_line((verify["content"] or {}).get("audio") or {})
        outcome.status = "ok" if verify["ok"] else "mismatch"
        outcome.detail = "; ".join(verify["problems"])
        evidence.write("result.json", {**result, "outcome": outcome.status, "detail": outcome.detail})
        ctx.produced[call.label] = {k: str(v) for k, v in artifacts.items()}
        return outcome
    except KeyboardInterrupt:
        # Recorded, then the invocation stops. A sent call is charged as one that never completed.
        if sent and message is None:
            settle("interrupted", no_completion(), error="interrupted while the Space was running it")
        evidence.write("result.json", {"outcome": "interrupted", "sent": sent,
                                       "completed": message is not None})
        raise
    except Exception as exc:  # noqa: BLE001 -- recorded, then the plan stops (or keeps going)
        outcome.status = "failed"
        outcome.category = outcome.category or "harness"
        outcome.detail = redact(f"{type(exc).__name__}: {exc}")
        if sent and message is None:
            settle("failed", no_completion(), error=outcome.detail, category=outcome.category)
        elif not sent:
            settle("failed", 0.0, error=outcome.detail, category=outcome.category)
        evidence.write("result.json", {"outcome": "failed", "sent": sent, "detail": outcome.detail,
                                       "traceback": redact("".join(traceback.format_exception(exc)))})
        return outcome
    finally:
        evidence.close()


def _duration(delta: timedelta) -> str:
    seconds = max(0, int(delta.total_seconds()))
    return f"{seconds // 3600}h {(seconds % 3600) // 60:02d}m"


# ---------------------------------------------------------------------------
# reporting
# ---------------------------------------------------------------------------


def schedule_days(costs: list[float], *, spent: float, ceiling: float) -> list[list[int]]:
    """The calls (by index) each UTC day can run at worst case, every call billed its full
    reservation: today first, from what the ledger already holds, then whole days from zero."""
    days: list[list[int]] = [[]]
    running = spent
    for index, cost in enumerate(costs):
        if running + cost > ceiling and (days[-1] or running > 0):
            days.append([])
            running = 0.0
        days[-1].append(index)
        running += cost
    return days


def _span(indexes: list[int], calls: list[Call], offset: int) -> str:
    first, last = indexes[0], indexes[-1]
    numbers = f"#{first + 1 + offset}" if first == last else f"#{first + 1 + offset}-{last + 1 + offset}"
    labels = calls[first].label if first == last else f"{calls[first].label} .. {calls[last].label}"
    return f"calls {numbers:<7} {labels}"


def _levels_line(call: Call) -> str | None:
    if call.app_level is None:
        return None
    from ltx25_studio import audio_conditioning

    name = "VIDEO_VARIATION_LEVELS" if _varies_clip(call.spec) else "VARIATION_LEVELS"
    levels = getattr(audio_conditioning, name, ())
    shown = f"; the mode's levels: {', '.join(f'{v:g}' for v in levels)}" if levels else ""
    return f"      level     {call.app_level:g} (noise_scale {call.value('noise_scale')!r}{shown})"


def _gate_line(call: Call) -> str | None:
    if call.gate == GATE_ACCEPTED:
        return "      app gate  generate.prepare_request accepts it, run as on the Space (ZeroGPU, CUDA)"
    if call.gate == GATE_DEFERRED:
        sources = ", ".join(f"{kind} from {call.media[kind].from_call}" for kind in call.chained)
        return (f"      app gate  checked when it exists: its {sources} is made by that call, and "
                f"generate.prepare_request runs on the file before anything is sent (a refusal bills nothing)")
    return None


def render_plan(calls: list[Call], *, floor_seconds: int | None, multiplier: float, spent: float,
                ceiling: float, live: bool, ledger_note: str, resume_command: str = "", skipped=(),
                offset: int = 0) -> list[str]:
    floor = (f", at least {floor_seconds} s (the Space's LTX25_GPU_DURATION_FLOOR_SECONDS)"
             if floor_seconds else "")
    lines = [
        "=== PLAN — LIVE: this WILL spend GPU-seconds ===" if live
        else "=== PLAN — DRY RUN: nothing is contacted and nothing is spent ===",
        f"  reservation from each call's size and length (the app's estimator){floor} · "
        f"multiplier x{multiplier:g} · ceiling {ceiling:.0f} GPU-s/UTC day",
        f"  {ledger_note}",
    ]
    if skipped:
        lines.append(f"  resume       {len(skipped)} call(s) already have an outcome in --out and are skipped, "
                     f"never re-run: " + ", ".join(f"{label} ({state})" for label, state in skipped))
    costs: list[float] = []
    running = spent
    for number, call in enumerate(calls, start=1 + offset):
        reserved = reservation_seconds(call, floor_seconds=floor_seconds)
        worst = reserved * multiplier
        costs.append(worst)
        running += worst
        parts = _reservation_parts(call, floor_seconds=floor_seconds)
        lines.append("")
        lines.append(f"  {number:>2}. {call.label}  [{call.mode}]")
        lines.append(f"      request   {json.dumps(resolved_request(call), sort_keys=False, default=str)}")
        for kind, item in call.media.items():
            if item.chained:
                where = f"chained from {item.from_call}" + (f" (frame {item.frame})" if kind == "image" else "")
            else:
                where = str(item.path)
            lines.append(f"      {kind:<9} {where}")
        for lora in call.loras:
            line = f"      lora      {lora.source} at {lora.weight:g} via {'upload' if lora.local else 'Hub id'}"
            if lora.needs_weight_route:
                line += (f": a strength other than {staged_lora_weight():g} needs a public route that sets it "
                         f"({LORA_WEIGHT_ENDPOINT} with index and weight). --live checks the Space for one "
                         f"before anything is spent and refuses the plan without it (exit 2); "
                         f"--skip {call.label} runs the rest")
            lines.append(line)
        gate = _gate_line(call)
        if gate:
            lines.append(gate)
        shown = [f"{key} {ran!r} (sent {sent!r})" for key, sent, ran in call.app_changes
                 if key in call.values or key in ("num_frames", "auto_duration")]
        if shown:
            lines.append(f"      app runs  {' · '.join(shown)}")
        level = _levels_line(call)
        if level:
            lines.append(level)
        for note in call.app_notes:
            lines.append(f"      app       {note}")
        geometry = call.expected_geometry()
        expect = {"geometry": f"{geometry[0]}x{geometry[1]}" if geometry else None,
                  "frames": call.expected_frames(),
                  **{k: v for k, v in call.expect.items() if k not in ("width", "height", "frames")}}
        lines.append(f"      expect    {json.dumps({k: v for k, v in expect.items() if v is not None})}")
        lines.append(f"      reserve   {reserved} s ({', '.join(parts)}) -> worst case <= {worst:.0f} GPU-s; "
                     f"day total <= {running:.0f}")
        if worst > ceiling:
            lines.append(f"      NEVER RUNS under this ceiling: its worst case alone passes {ceiling:.0f} GPU-s")
    plan_total = running - spent
    days = schedule_days(costs, spent=spent, ceiling=ceiling)
    today = _utcnow()
    lines += [
        "",
        f"  this plan    <= {plan_total:.0f} GPU-s worst case (every call at its full reservation)",
        f"  today        <= {spent:.1f} GPU-s already on the ledger (UTC {utc_day(today)})",
        f"  remaining    {max(0.0, ceiling - spent):.1f} GPU-s before the {ceiling:.0f} ceiling",
        f"  days         {len(days)} UTC day(s) at worst case under the {ceiling:.0f} GPU-s ceiling:",
    ]
    for index, members in enumerate(days):
        when = utc_day(today + timedelta(days=index)) + (" (today)" if index == 0 else "")
        if not members:
            lines.append(f"                 day {index + 1}  UTC {when:<18}  nothing fits in what is left today")
            continue
        total = sum(costs[i] for i in members)
        lines.append(f"                 day {index + 1}  UTC {when:<18}  <= {total:>4.0f} GPU-s  "
                     f"{_span(members, calls, offset)}")
    if len(days) == 1:
        lines.append(f"  verdict      FITS, with {ceiling - running:.0f} GPU-s to spare at worst case")
    else:
        stop = days[1][0]
        lines.append(
            f"  verdict      DOES NOT FIT: call #{stop + 1 + offset} ({calls[stop].label}) is where the ceiling "
            f"binds at worst case. A live run checks the ledger before every call and refuses, with exit 3 "
            f"and nothing sent, the first call that could pass the ceiling; a call bills its measured window, "
            f"usually far less than its reservation, so a live day usually runs further than this. The UTC day "
            f"rolls over at {next_utc_midnight(today).isoformat().replace('+00:00', 'Z')}."
        )
        if resume_command:
            lines += [
                "  resume       on the next UTC day, run the same plan with --resume: every call that has an "
                "outcome in --out is skipped by label, never re-run, and the rest runs:",
                f"                 {resume_command}",
            ]
    lines.append("  Every figure is an UPPER BOUND, not measured billing.")
    return lines


# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------

_FIELD_FLAGS = (
    ("--prompt", "prompt", str),
    ("--width", "width", int),
    ("--height", "height", int),
    ("--num-frames", "num_frames", int),
    ("--seed", "seed", int),
    ("--min-seconds", "min_seconds", float),
    ("--max-seconds", "max_seconds", float),
    ("--strength", "strength", float),
    ("--noise-scale", "noise_scale", float),
)
_BOOL_FLAGS = (
    ("--enable-stage-2", "enable_stage_2"),
    ("--enhance-prompt", "enhance_prompt"),
    ("--auto-duration", "auto_duration"),
)

SUBCOMMANDS = ("status", "info", "logs")

_EPILOG = f"""
read-only subcommands (one GET each, no GPU time; see `<command> --help`):
  status            the Space's runtime stage, hardware and deployed commit
  info              compare the Space's /gradio_api/info for {ENDPOINT} with the local field spec
  logs --out FILE   save the Space's runtime (or build) log

plan file: a JSON list of calls, or {{"defaults": {{...}}, "calls": [...]}}. A call has a "label",
a "mode", any {ENDPOINT} field by its key (a choice by its value or its label, e.g. "decoder":
"Sharper (standard decoder)"), input media as file paths under --assets-dir ("image", "video", "audio") or
{{"from_call": "<earlier label>", "frame": 0}}, optional "loras" ([{{"source": "<hub id>" |
"<file.safetensors>", "weight": 0.8}}]) and optional "expect" ({", ".join(sorted(_EXPECT_KEYS))}).
Every call whose inputs exist goes through the app's own gate (generate.prepare_request) in the
dry run, as the Space runs it; a call with an input made by an earlier call is gated when that
file exists, before anything is sent.

a plan longer than one day: run it with --out DIR; when it stops at the ceiling (exit 3), run the
same command with --resume on the next UTC day. Calls with an outcome in DIR are skipped by
label, never re-run; a failed one is re-run only if you move its folder out of DIR.

exit codes: 0 plan complete · 2 illegal plan or contract mismatch (nothing spent) · 3 stopped at
the ceiling · 4 platform quota refusal · 5 a call failed (still 5 with --keep-going)
"""


def _run_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(
        prog="space_matrix.py",
        description="Run a plan of calls against the private LTX-2.5 Studio Space inside a hard daily "
        "GPU budget. Dry run by default; --live is what spends.",
        epilog=_EPILOG,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    plan = parser.add_argument_group("what to run")
    plan.add_argument("--plan", help="a plan JSON file, or 'default' for the historical 11-mode loop "
                      "(the default when no --mode is given)")
    plan.add_argument("--modes", help="with --plan default: comma-separated mode ids, run in that order")
    plan.add_argument("--only", help="comma-separated call labels: run only these calls of the plan")
    plan.add_argument("--skip", help="comma-separated call labels: leave these calls of the plan out")
    plan.add_argument("--assets-dir", help="where relative media and LoRA paths resolve (default: the plan "
                      "file's folder)")
    plan.add_argument("--mode", help="a single call from flags: the mode id")
    plan.add_argument("--label", help="with --mode: the call's label (default: the mode id)")
    for flag, key, kind in _FIELD_FLAGS:
        plan.add_argument(flag, type=kind, default=None, help=f"the {key} field (--mode, or --plan default)")
    for flag, key in _BOOL_FLAGS:
        plan.add_argument(flag, action="store_true", default=None, help=f"set {key} (--mode, or --plan default)")
    plan.add_argument("--image", help="with --mode: the input image file")
    plan.add_argument("--video", help="with --mode: the input video file")
    plan.add_argument("--audio", help="with --mode: the input audio file")
    plan.add_argument("--set", action="append", default=[], metavar="KEY=VALUE",
                      help="with --mode: any field by key (JSON value, else a string); repeatable")
    plan.add_argument("--lora", action="append", default=[], metavar="SOURCE[@WEIGHT]",
                      help="with --mode: stage a LoRA (Hub id or local .safetensors); repeatable")
    plan.add_argument("--expect", action="append", default=[], metavar="KEY=VALUE",
                      help=f"with --mode: an expectation ({', '.join(sorted(_EXPECT_KEYS))}); repeatable")

    run = parser.add_argument_group("running and budget")
    run.add_argument("--live", action="store_true", help="actually call the Space. Without it nothing is "
                     "contacted and nothing is spent.")
    run.add_argument("--dry-run", action="store_true", help="force the dry run even with --live")
    run.add_argument("--quota-ledger", help="the append-only spend ledger (one JSON record per line, keyed by "
                     "UTC day). Required with --live; read, never written, by a dry run.")
    run.add_argument("--quota-ceiling", type=float, default=DEFAULT_QUOTA_CEILING_SECONDS,
                     help=f"GPU-seconds per UTC day never to pass (default {DEFAULT_QUOTA_CEILING_SECONDS:.0f})")
    run.add_argument("--reservation-floor-seconds", type=int, default=None,
                     help="the Space's LTX25_GPU_DURATION_FLOOR_SECONDS, if it sets one: every call's "
                     "reservation is at least this (default: unset, as on the Space). The reservation "
                     "itself is the app's estimate from each call's size and length")
    run.add_argument("--zerogpu-size", default=DEFAULT_ZEROGPU_SIZE,
                     help=f"the Space's LTX25_ZEROGPU_SIZE; zerogpu.quota_multiplier turns it into the quota "
                     f"factor (default {DEFAULT_ZEROGPU_SIZE}, which counts double)")
    run.add_argument("--out", help="evidence directory, one folder per call label. Required with --live.")
    run.add_argument("--resume", action="store_true", help="continue a plan in its --out: every call with an "
                     "outcome there is skipped by label and never re-run (a dry run shows what is left)")
    run.add_argument("--keep-going", action="store_true", help="after a failed call, run the rest of the plan "
                     "(a quota refusal or the ceiling still stops at once)")
    run.add_argument("--call-timeout", type=float, default=DEFAULT_CALL_TIMEOUT_SECONDS,
                     help=f"seconds to wait for one call to complete (default {DEFAULT_CALL_TIMEOUT_SECONDS:.0f}); "
                     "a timed-out call is recorded, charged at least its reservation, and never retried")
    run.add_argument("--read-timeout", type=float, default=DEFAULT_READ_TIMEOUT_SECONDS,
                     help=f"seconds of silence on the event stream before giving up (default "
                     f"{DEFAULT_READ_TIMEOUT_SECONDS:.0f})")
    run.add_argument("--sampling-rate", type=int, default=PINNED_VOCODER_SAMPLING_RATE,
                     help=f"expected audio sample rate (default {PINNED_VOCODER_SAMPLING_RATE}); "
                     "expect.sample_rate overrides it per call")

    where = parser.add_argument_group("where")
    _where_flags(where)
    return parser


def _where_flags(group) -> None:
    group.add_argument("--space", default=DEFAULT_SPACE_ID, help=f"the Space id (default {DEFAULT_SPACE_ID})")
    group.add_argument("--space-url", help="the Space app's base URL (default: https://<owner>-<name>.hf.space)")
    group.add_argument("--hub-url", default=DEFAULT_HUB_URL, help=f"the Hub API base (default {DEFAULT_HUB_URL})")


def _sub_parser(name: str) -> argparse.ArgumentParser:
    descriptions = {
        "status": "Print the Space's runtime stage, hardware and deployed commit. One GET to the Hub API.",
        "info": f"Compare the Space's /gradio_api/info for {ENDPOINT} with the local field spec. One GET to "
                "the Space. Exits 2 on a mismatch.",
        "logs": "Save the Space's runtime (or build) log to a file. One GET to the Hub API, read for at most "
                "--max-seconds.",
    }
    parser = argparse.ArgumentParser(prog=f"space_matrix.py {name}", description=descriptions[name])
    _where_flags(parser)
    if name == "logs":
        parser.add_argument("--out", required=True, help="the file to write")
        parser.add_argument("--kind", choices=("run", "build"), default="run", help="which log (default run)")
        parser.add_argument("--max-seconds", type=float, default=20.0,
                            help="stop reading the (endless) log stream after this long (default 20)")
    return parser


def _parse_kv(items: list[str], what: str) -> dict:
    out = {}
    for item in items:
        key, sep, raw = item.partition("=")
        if not sep or not key:
            raise PlanError(f"{what} {item!r} must be KEY=VALUE")
        try:
            out[key] = json.loads(raw)
        except ValueError:
            out[key] = raw
    return out


def _flag_values(args) -> dict:
    values = {key: getattr(args, flag[2:].replace("-", "_")) for flag, key, _ in _FIELD_FLAGS}
    values.update({key: getattr(args, flag[2:].replace("-", "_")) for flag, key in _BOOL_FLAGS})
    return {k: v for k, v in values.items() if v is not None}


def _labels(text: str | None) -> list[str]:
    return [label.strip() for label in (text or "").split(",") if label.strip()]


def build_calls(args) -> list[Call]:
    flags = _flag_values(args)
    single = args.mode is not None
    singles = [args.image, args.video, args.audio, args.label, *args.set, *args.lora, *args.expect]
    if not single and any(singles):
        raise PlanError("--image/--video/--audio/--label/--set/--lora/--expect describe a single call: add --mode")
    if single and (args.plan or args.modes):
        raise PlanError("--mode describes one call; it cannot be combined with --plan or --modes")
    if single:
        entry: dict = {"label": args.label or args.mode, "mode": args.mode, **flags, **_parse_kv(args.set, "--set")}
        for kind in MEDIA_KINDS:
            if getattr(args, kind):
                entry[kind] = getattr(args, kind)
        loras = []
        for item in args.lora:
            source, sep, weight = item.rpartition("@")
            loras.append({"source": source, "weight": float(weight)} if sep and _is_number(weight)
                         else {"source": item})
        if loras:
            entry["loras"] = loras
        if args.expect:
            entry["expect"] = _parse_kv(args.expect, "--expect")
        base = Path(args.assets_dir).expanduser() if args.assets_dir else Path.cwd()
        calls = parse_plan([entry], base=base)
    elif args.plan in (None, "default"):
        mode_ids = [m.strip() for m in args.modes.split(",") if m.strip()] if args.modes else None
        calls = default_plan(mode_ids, flags)
    else:
        if args.modes:
            raise PlanError("--modes selects from --plan default; use --only with a plan file")
        if flags:
            raise PlanError(f"{', '.join('--' + k.replace('_', '-') for k in flags)}: a plan file sets its own "
                            "fields; edit the file")
        calls = load_plan(args.plan, assets_dir=Path(args.assets_dir).expanduser() if args.assets_dir else None)
    known = {c.label for c in calls}
    for flag, wanted in (("--only", _labels(args.only)), ("--skip", _labels(args.skip))):
        unknown = [label for label in wanted if label not in known]
        if unknown:
            raise PlanError(f"{flag}: no call labelled {unknown} (labels: {sorted(known)})")
    if args.only:
        calls = [c for c in calls if c.label in _labels(args.only)]
    if args.skip:
        calls = [c for c in calls if c.label not in _labels(args.skip)]
    chosen = {c.label for c in calls}
    for call in calls:
        for item in call.media.values():
            if item.chained and item.from_call not in chosen:
                raise PlanError(f"{call.label} chains from {item.from_call}, which --only/--skip leaves out")
    return calls


def _is_number(text: str) -> bool:
    try:
        float(text)
    except ValueError:
        return False
    return True


# ---------------------------------------------------------------------------
# resuming a plan in its --out
# ---------------------------------------------------------------------------


def evidence_state(folder: Path) -> dict | None:
    """What an earlier invocation left in ``--out/<label>/``; None when it holds nothing."""
    if not folder.is_dir() or not any(folder.iterdir()):
        return None
    try:
        result = json.loads((folder / "result.json").read_text())
    except FileNotFoundError:
        # A call writes result.json before it is sent: nothing of this one reached the Space.
        return {"outcome": "unsent", "sent": False}
    except (OSError, ValueError):
        return {"outcome": "unreadable", "sent": True}
    if not isinstance(result, dict):
        return {"outcome": "unreadable", "sent": True}
    artifacts = {track: info.get("path") for track, info in (result.get("artifacts") or {}).items()
                 if isinstance(info, dict) and info.get("path")}
    return {"outcome": str(result.get("outcome") or "unknown"), "sent": result.get("sent", True) is not False,
            "artifacts": artifacts}


def _runs_again(state: dict | None) -> bool:
    """Only a call nothing of which reached the Space runs on --resume; nothing is retried."""
    return state is None or state["outcome"] == "unsent" or (state["outcome"] == "interrupted" and not state["sent"])


def resume_split(out_dir: Path, calls: list[Call]) -> tuple[list[Call], list[tuple[str, str]], dict]:
    """``(calls still to run, (label, outcome) of those skipped, artifacts of the finished ones)``."""
    remaining, skipped, produced = [], [], {}
    for call in calls:
        state = evidence_state(out_dir / call.label)
        if _runs_again(state):
            remaining.append(call)
            continue
        skipped.append((call.label, state["outcome"]))
        if state["outcome"] in ("ok", "mismatch") and state.get("artifacts"):
            produced[call.label] = state["artifacts"]
    return remaining, skipped, produced


def _resume_command(argv: list[str]) -> str:
    """The command that continues this plan on the next UTC day."""
    import shlex

    words = [w for w in argv if w not in ("--dry-run", "--live", "--resume")]
    extra = []
    if not any(w == "--out" or w.startswith("--out=") for w in words):
        extra += ["--out", "<DIR>"]
    if not any(w == "--quota-ledger" or w.startswith("--quota-ledger=") for w in words):
        extra += ["--quota-ledger", "<LEDGER>"]
    return " ".join(["python scripts/space_matrix.py", shlex.join(words), *extra, "--live", "--resume"]).replace("  ", " ")


class _Out:
    """Every line the CLI prints passes through here and is redacted."""

    def __init__(self, log):
        self._log = log

    def __call__(self, line: Any = "") -> None:
        self._log(redact(line))


def main(argv=None, *, log=print) -> int:
    out = _Out(log)
    argv = list(sys.argv[1:] if argv is None else argv)
    if argv and argv[0] in SUBCOMMANDS:
        return _subcommand(argv[0], argv[1:], out)
    args = _run_parser().parse_args(argv)
    try:
        calls = build_calls(args)
    except PlanError as exc:
        out(f"the plan is not runnable:\n{exc}")
        return EXIT_ILLEGAL
    live = args.live and not args.dry_run
    try:
        multiplier = quota_multiplier(args.zerogpu_size)
    except Exception as exc:  # noqa: BLE001 -- the app's ConfigError names the legal sizes
        out(f"--zerogpu-size: {exc}")
        return EXIT_ILLEGAL
    if args.reservation_floor_seconds is not None and args.reservation_floor_seconds < 1:
        out("--reservation-floor-seconds must be a whole number of seconds >= 1, as on the Space.")
        return EXIT_ILLEGAL

    ledger = Ledger(args.quota_ledger) if args.quota_ledger else None
    if live and ledger is None:
        out("--live needs --quota-ledger PATH: the day's spend must be kept outside any checkout.")
        return EXIT_ILLEGAL
    if live and not args.out:
        out("--live needs --out DIR for the per-call evidence.")
        return EXIT_ILLEGAL
    if args.resume and not args.out:
        out("--resume needs --out DIR: the evidence folder of the run it continues.")
        return EXIT_ILLEGAL
    skipped: list = []
    produced: dict = {}
    if args.resume:
        calls, skipped, produced = resume_split(Path(args.out).expanduser(), calls)
    try:
        spent = ledger.spent(utc_day()) if ledger else 0.0
        previous = ledger.previous_day(utc_day()) if ledger else None
        unsettled = ledger.unsettled() if ledger else []
    except QuotaLedgerUnreadable as exc:
        out(str(exc))
        return EXIT_ILLEGAL
    if ledger is None:
        note = "no --quota-ledger given: this dry run assumes nothing has been spent today"
    else:
        note = f"ledger {ledger.path}: <= {spent:.1f} GPU-s on UTC {utc_day()}"
        if previous:
            note += f" (the last earlier day, UTC {previous[0]}, ended at <= {previous[1]:.1f})"
        if unsettled:
            note += (f"; {len(unsettled)} call(s) sent and never settled ("
                     f"{', '.join(str(r.get('label')) for r in unsettled)}) count at their full reservation")
    if not calls:
        out(f"  {note}")
        out(f"nothing left to run: every call of the plan has an outcome in {args.out}"
            if skipped else "nothing to run")
        return EXIT_OK
    for line in render_plan(calls, floor_seconds=args.reservation_floor_seconds, multiplier=multiplier, spent=spent,
                            ceiling=args.quota_ceiling, live=live, ledger_note=note,
                            resume_command=_resume_command(argv), skipped=skipped):
        out(line)
    if not live:
        out("")
        out("DRY RUN: every call above was validated offline. Nothing was contacted, ZERO GPU-seconds were "
            "spent, and the ledger was read, not written. Add --live to execute.")
        return EXIT_OK
    return _live(args, calls, ledger, multiplier, out, skipped=skipped, produced=produced,
                 resume_command=_resume_command(argv))


def _live(args, calls: list[Call], ledger: Ledger, multiplier: float, out, *, skipped=(), produced=None,
          resume_command: str = "") -> int:
    out_dir = Path(args.out).expanduser()
    if not args.resume:
        taken = [c.label for c in calls if (out_dir / c.label).exists() and any((out_dir / c.label).iterdir())]
        if taken:
            out(f"{out_dir} already holds evidence for {taken}; use a fresh --out so runs never mix, or "
                f"--resume to run only the calls that have no outcome there.")
            return EXIT_ILLEGAL
    secret = read_token()
    if secret is None:
        out(f"no token: set HF_TOKEN or log in so {token_path()} exists. The Space is private.")
        return EXIT_ILLEGAL
    http = Http(secret)
    hub = Hub(args.hub_url, http)
    try:
        status = hub.space(args.space)
    except (HttpError, TransportError) as exc:
        out(f"could not read the Space's status: {exc}")
        return EXIT_ILLEGAL
    runtime = status.get("runtime") or {}
    stage = runtime.get("stage")
    if stage != "RUNNING":
        out(f"the Space is {stage}, not RUNNING. Wake it or wait for the build; nothing was sent.")
        return EXIT_ILLEGAL
    base_url = args.space_url or status.get("host") or space_host(args.space)
    if not base_url.startswith(("http://", "https://")):
        base_url = "https://" + base_url
    sha = runtime.get("sha") or status.get("sha")
    try:
        app = SpaceApp(base_url, http)
        config = app.config()
        app.prefix = "/" + str(config.get("api_prefix") or API_PREFIX).strip("/")
        info = app.info()
    except (HttpError, TransportError, ValueError) as exc:
        out(f"could not read the Space's API: {exc}")
        return EXIT_ILLEGAL
    generate = find_endpoint(config, info, ENDPOINT)
    problems: list[str] = []
    table: list[str] = []
    if generate is None:
        problems.append(f"the Space publishes no {ENDPOINT}")
    else:
        problems, table = generate_contract_problems(generate.parameters, input_types=generate.input_types,
                                                     plan_modes=[c.mode for c in calls])
    route = _lora_route(config, info)
    problems += lora_contract_problems(route, calls)
    out_dir.mkdir(parents=True, exist_ok=True)
    (out_dir / "contract.json").write_text(_redacted_json(
        {"space": args.space, "space_url": base_url, "space_sha": sha, "problems": problems, "table": table,
         "info": info.get("named_endpoints", {}).get(ENDPOINT)}))
    if problems:
        out(f"\nCONTRACT MISMATCH: the Space at {sha or '?'} does not take what this checkout's field spec builds. "
            "Nothing was spent.")
        for line in table:
            out(line)
        for problem in problems:
            out(f"  ✗ {problem}")
        return EXIT_ILLEGAL
    out(f"\ncontract     {ENDPOINT} matches the field spec ({len(generate.parameters)} parameters) · Space "
        f"commit {sha or '?'} · {base_url}")

    invocation = uuid.uuid4().hex[:12]
    ledger.append({"kind": "invocation", "utc_day": utc_day(), "at": _utcnow().isoformat(), "invocation": invocation,
                   "space": args.space, "space_sha": sha, "calls": [c.label for c in calls],
                   "skipped": [label for label, _state in skipped], "out": str(out_dir)})
    ctx = Session(app=app, generate=generate, lora=route, ledger=ledger, out=out_dir, log=out,
                  invocation=invocation, ceiling=args.quota_ceiling, floor_seconds=args.reservation_floor_seconds,
                  multiplier=multiplier, call_timeout=args.call_timeout, read_timeout=args.read_timeout,
                  sampling_rate=args.sampling_rate, space_sha=sha, produced=dict(produced or {}))
    for label, state in skipped:
        out(f"  {label:<32} skipped: it has an outcome in {out_dir} ({state})")
    outcomes: list[Outcome] = []
    code = EXIT_OK
    stop = ""
    for call in calls:
        folder = out_dir / call.label
        if folder.exists() and any(folder.iterdir()):
            # --resume: an earlier attempt nothing of which reached the Space; kept, moved aside.
            aside = folder.with_name(f"{folder.name}.unsent-{invocation}")
            folder.rename(aside)
            out(f"  {call.label}: its earlier attempt was never sent; its folder is now {aside.name}")
        try:
            outcome = run_call(ctx, call)
        except CeilingReached as exc:
            stop = str(exc)
            code = EXIT_CEILING
            out(f"\n*** STOPPED AT THE CEILING ***\n  {exc}")
            break
        except QuotaLedgerUnreadable as exc:
            stop = str(exc)
            code = EXIT_ILLEGAL
            out(f"\n*** STOPPED: the ledger became unreadable ***\n  {exc}")
            break
        outcomes.append(outcome)
        out(f"  -> {outcome.status.upper()}  billed <= {outcome.billed_conservative:.1f} GPU-s (conservative)"
            + (f"  [{outcome.category}]" if outcome.category else "")
            + (f"  {outcome.detail}" if outcome.detail else "")
            + (f"  flags: {', '.join(outcome.flags)}" if outcome.flags else ""))
        if outcome.audio:
            out(f"     audio  {outcome.audio}  (measurements, no verdict)")
        if outcome.status == "quota":
            stop = outcome.detail
            code = EXIT_QUOTA
            out("\n*** THE PLATFORM REFUSED ON QUOTA: stopping now. Nothing is retried. ***")
            break
        if outcome.failed:
            code = EXIT_CALL_FAILED
            if not args.keep_going:
                stop = f"{call.label} {outcome.status}"
                out("  stopping: a call failed (pass --keep-going to run the rest)")
                break
    day = utc_day()
    try:
        spent_today = ledger.spent(day)
    except QuotaLedgerUnreadable:
        spent_today = float("nan")
    this_run = sum(o.billed_conservative for o in outcomes)
    summary = {
        "invocation": invocation,
        "space_sha": sha,
        "exit_code": code,
        "stopped": stop or None,
        "skipped": [{"label": label, "outcome": state} for label, state in skipped],
        "calls": [{"label": o.label, "status": o.status, "category": o.category, "detail": o.detail,
                   "reserved_seconds": o.reserved, "billed_conservative": round(o.billed_conservative, 3),
                   "flags": o.flags, "audio": o.audio or None} for o in outcomes],
        "not_run": [c.label for c in calls[len(outcomes):]],
        "this_invocation_billed_conservative": round(this_run, 3),
        "utc_day": day,
        "spent_today_conservative": round(spent_today, 3),
        "ceiling": args.quota_ceiling,
    }
    (out_dir / "summary.json").write_text(_redacted_json(summary))
    (out_dir / f"summary-{invocation}.json").write_text(_redacted_json(summary))
    out("\n=== SUMMARY ===")
    for o in outcomes:
        out(f"  {o.label:<32} {o.status:<9} <= {o.billed_conservative:7.1f} GPU-s"
            + (f"  [{'; '.join(o.flags)}]" if o.flags else ""))
    for label in summary["not_run"]:
        out(f"  {label:<32} not run")
    out(f"  this invocation <= {this_run:.1f} GPU-s · today (UTC {day}) <= {spent_today:.1f} of {args.quota_ceiling:.0f}"
        " · UPPER BOUNDS from wall clock, not measured billing")
    out(f"  evidence {out_dir} · ledger {ledger.path}")
    if summary["not_run"] and code == EXIT_CEILING and resume_command:
        out(f"  the rest runs after the UTC day rolls over, with:\n    {resume_command}")
    return code


def _subcommand(name: str, argv: list[str], out) -> int:
    args = _sub_parser(name).parse_args(argv)
    http = Http(read_token())
    try:
        if name == "status":
            payload = Hub(args.hub_url, http).space(args.space)
            runtime = payload.get("runtime") or {}
            hardware = runtime.get("hardware") or {}
            out(f"space      {payload.get('id', args.space)}{' (private)' if payload.get('private') else ''}")
            out(f"stage      {runtime.get('stage')}")
            out(f"hardware   {hardware.get('current')} (requested {hardware.get('requested')})")
            out(f"commit     {payload.get('sha')} (repo head) · running {runtime.get('sha')}")
            out(f"host       {payload.get('host') or space_host(args.space)}")
            return EXIT_OK
        if name == "logs":
            lines = Hub(args.hub_url, http).logs(args.space, kind=args.kind, max_seconds=args.max_seconds)
            target = Path(args.out).expanduser()
            target.parent.mkdir(parents=True, exist_ok=True)
            target.write_text(redact("\n".join(lines) + ("\n" if lines else "")))
            out(f"wrote {len(lines)} {args.kind}-log line(s) to {target}")
            return EXIT_OK
        app = SpaceApp(args.space_url or space_host(args.space), http)
        info = app.info()
        published = (info.get("named_endpoints") or {}).get(ENDPOINT)
        if published is None:
            out(f"the Space publishes no {ENDPOINT}")
            return EXIT_ILLEGAL
        problems, table = generate_contract_problems(list(published.get("parameters") or []))
        for line in table:
            out(line)
        for problem in problems:
            out(f"  ✗ {problem}")
        out("MATCHES the local field spec" if not problems else "MISMATCH: a --live run would refuse")
        return EXIT_OK if not problems else EXIT_ILLEGAL
    except (HttpError, TransportError, ValueError) as exc:
        out(f"{name}: {exc}")
        return 1


def _entry() -> int:
    try:
        return main()
    except KeyboardInterrupt:
        print("interrupted", file=sys.stderr)
        return 130
    except Exception as exc:  # noqa: BLE001 -- the traceback is formatted here, so it is redacted here
        print(redact("".join(traceback.format_exception(exc))), file=sys.stderr)
        return 1


if __name__ == "__main__":
    raise SystemExit(_entry())