File size: 243,886 Bytes
7e7df2a
 
86b8987
7e7df2a
 
 
 
 
 
e99cd17
1a6ae0b
fe62dd7
7e7df2a
 
 
401d0bc
7e7df2a
f980128
7e7df2a
 
 
 
 
 
5d30c6f
347b364
 
 
 
 
 
7e7df2a
 
86b8987
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f20fb7
86b8987
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
 
 
 
 
 
 
 
 
c854ffa
7e7df2a
 
 
 
c854ffa
401d0bc
 
 
b9b02f6
 
 
 
 
 
fe62dd7
 
f980128
fe62dd7
 
980c9b8
fe62dd7
 
 
 
 
347b364
fe62dd7
980c9b8
b9b02f6
 
 
fe62dd7
 
 
 
 
 
 
 
 
 
f980128
fe62dd7
980c9b8
fe62dd7
 
 
 
 
f980128
 
fe62dd7
 
 
f980128
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
b9b02f6
 
7e7df2a
 
1a6ae0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
980c9b8
 
 
 
 
b9b02f6
 
980c9b8
1a6ae0b
980c9b8
 
 
 
 
7e7df2a
 
 
980c9b8
7e7df2a
 
 
980c9b8
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
980c9b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
980c9b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c854ffa
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d30c6f
f980128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c854ffa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86b8987
 
7e7df2a
 
 
 
 
 
 
86b8987
e99cd17
 
7e7df2a
 
 
 
86b8987
e99cd17
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e99cd17
 
7e7df2a
 
e99cd17
 
86b8987
7e7df2a
 
 
 
 
 
86b8987
7e7df2a
 
 
 
 
 
 
86b8987
e99cd17
 
 
 
86b8987
 
e99cd17
86b8987
 
 
e99cd17
 
 
 
 
 
86b8987
7e7df2a
 
86b8987
 
 
 
 
 
7e7df2a
 
86b8987
7e7df2a
 
 
 
 
 
 
 
 
86b8987
7e7df2a
 
86b8987
f980128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86b8987
f980128
 
 
 
 
 
 
 
 
 
 
 
 
 
86b8987
f980128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86b8987
7e7df2a
 
86b8987
 
7e7df2a
86b8987
7e7df2a
f980128
c854ffa
b392cf8
 
86b8987
b392cf8
 
c854ffa
7e7df2a
 
 
8f20fb7
 
 
 
 
 
 
86b8987
8f20fb7
7e7df2a
86b8987
7e7df2a
86b8987
f980128
86b8987
f980128
 
86b8987
f980128
8f20fb7
f980128
 
86b8987
f980128
 
86b8987
f980128
86b8987
b392cf8
 
86b8987
 
b392cf8
8f20fb7
b392cf8
 
 
7e7df2a
f980128
c854ffa
f980128
c854ffa
7e7df2a
c854ffa
 
f980128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
8f20fb7
f980128
7e7df2a
 
 
 
 
f980128
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
8f20fb7
f980128
 
 
 
 
 
7e7df2a
86b8987
7e7df2a
 
 
 
86b8987
7e7df2a
 
 
86b8987
 
 
 
7e7df2a
86b8987
7e7df2a
86b8987
7e7df2a
 
 
86b8987
 
 
 
7e7df2a
 
 
 
 
 
 
86b8987
 
 
 
 
 
8f20fb7
 
 
86b8987
f980128
86b8987
 
f980128
 
 
 
86b8987
f980128
86b8987
f980128
 
 
 
 
 
 
 
86b8987
 
 
 
 
 
 
 
 
 
f980128
 
 
 
 
 
 
 
7e7df2a
 
86b8987
 
5d30c6f
 
 
86b8987
 
 
 
5d30c6f
86b8987
5d30c6f
86b8987
5d30c6f
86b8987
5d30c6f
 
 
 
 
 
 
 
 
 
 
86b8987
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e99cd17
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86b8987
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d30c6f
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d30c6f
c854ffa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
b9b02f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d30c6f
980c9b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
f980128
 
 
7e7df2a
 
 
 
 
 
 
f980128
7e7df2a
 
 
 
f980128
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
b9b02f6
 
 
 
7e7df2a
 
fe62dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
fe62dd7
 
 
 
f980128
 
 
 
 
fe62dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0a09b6
 
 
 
 
 
 
 
 
 
980c9b8
 
 
 
 
 
 
 
 
1a6ae0b
 
ca45ff1
b9b02f6
 
 
 
980c9b8
 
fe62dd7
 
 
 
 
 
 
 
 
 
 
980c9b8
 
1a6ae0b
 
ca45ff1
b9b02f6
 
 
 
980c9b8
 
 
b0a09b6
fe62dd7
f980128
fe62dd7
 
 
 
c761fba
fe62dd7
 
 
 
 
 
 
 
 
c761fba
 
 
 
 
 
fe62dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
fe62dd7
 
 
 
f980128
 
 
 
 
fe62dd7
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
f980128
 
7e7df2a
e99cd17
7e7df2a
 
 
 
f980128
 
 
 
 
 
7e7df2a
 
9e5bb0c
7e7df2a
 
 
 
 
 
 
 
 
 
f980128
 
 
 
 
7e7df2a
 
 
 
 
 
f980128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e99cd17
 
 
f980128
 
 
9e5bb0c
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
980c9b8
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
9e5bb0c
 
 
 
 
f980128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e99cd17
 
 
 
 
 
f980128
 
9e5bb0c
f980128
 
 
 
 
 
 
 
 
 
 
 
 
e99cd17
 
 
 
 
 
 
 
 
 
 
7e7df2a
e99cd17
7e7df2a
 
 
 
e99cd17
9e5bb0c
e99cd17
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
 
 
7e7df2a
 
 
 
 
 
 
f980128
7e7df2a
 
 
 
f980128
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
 
f980128
fe62dd7
 
b0a09b6
fe62dd7
f980128
fe62dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e99cd17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e5bb0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
f980128
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
f980128
 
 
7e7df2a
f980128
 
 
 
7e7df2a
 
 
 
 
 
f980128
7e7df2a
 
 
 
f980128
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0a09b6
 
 
 
 
 
 
86b8987
b0a09b6
86b8987
 
 
b0a09b6
 
86b8987
 
 
b0a09b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
fe62dd7
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c761fba
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
fe62dd7
347b364
 
 
fe62dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
fe62dd7
 
 
 
f980128
 
 
 
 
fe62dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0a09b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
 
 
1a6ae0b
 
 
 
 
 
 
 
 
 
fe62dd7
 
 
1a6ae0b
fe62dd7
 
 
 
 
980c9b8
 
1a6ae0b
 
ca45ff1
1a6ae0b
b9b02f6
 
 
 
 
 
980c9b8
 
 
fe62dd7
 
 
 
 
b0a09b6
 
 
 
 
 
 
 
fe62dd7
 
 
f980128
 
 
fe62dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
980c9b8
 
 
 
 
 
 
 
 
 
1a6ae0b
980c9b8
1a6ae0b
ca45ff1
b9b02f6
 
 
 
980c9b8
b9b02f6
980c9b8
 
 
 
 
 
 
 
 
1a6ae0b
980c9b8
1a6ae0b
ca45ff1
b9b02f6
 
 
 
 
980c9b8
 
 
 
 
 
 
 
 
 
1a6ae0b
980c9b8
1a6ae0b
ca45ff1
b9b02f6
 
 
 
 
980c9b8
1a6ae0b
 
 
 
 
980c9b8
 
 
1a6ae0b
980c9b8
1a6ae0b
 
 
980c9b8
1a6ae0b
 
 
 
 
ca45ff1
 
 
 
 
b9b02f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
980c9b8
 
b9b02f6
980c9b8
 
 
1a6ae0b
980c9b8
1a6ae0b
ca45ff1
b9b02f6
 
 
 
980c9b8
 
 
b9b02f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
 
 
 
980c9b8
 
fe62dd7
 
f980128
fe62dd7
 
 
 
 
 
 
980c9b8
f980128
fe62dd7
980c9b8
fe62dd7
 
f980128
fe62dd7
 
f980128
980c9b8
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
b9b02f6
 
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347b364
 
 
 
 
 
 
980c9b8
 
 
 
fe62dd7
 
980c9b8
 
 
1a6ae0b
 
ca45ff1
980c9b8
 
 
 
 
b9b02f6
 
 
 
980c9b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
 
 
980c9b8
 
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
980c9b8
b9b02f6
980c9b8
 
fe62dd7
 
 
 
 
 
 
 
 
f980128
 
 
fe62dd7
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347b364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
980c9b8
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
 
c854ffa
 
7e7df2a
c854ffa
7e7df2a
 
 
 
 
 
 
 
 
 
 
f980128
 
7e7df2a
 
 
 
c854ffa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
b0a09b6
fe62dd7
f980128
980c9b8
fe62dd7
 
 
 
f980128
 
b0a09b6
f980128
 
 
fe62dd7
 
b0a09b6
fe62dd7
f980128
fe62dd7
 
b0a09b6
fe62dd7
f980128
fe62dd7
 
b0a09b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
 
 
 
 
f980128
 
 
 
 
b0a09b6
 
 
 
 
 
fe62dd7
 
980c9b8
 
 
 
 
1a6ae0b
 
b9b02f6
980c9b8
 
 
 
 
 
 
 
 
 
 
 
 
 
1a6ae0b
 
 
 
 
 
 
 
 
 
 
 
ca45ff1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9b02f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a6ae0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
980c9b8
 
 
1a6ae0b
 
 
980c9b8
 
1a6ae0b
980c9b8
 
1a6ae0b
980c9b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a6ae0b
 
 
 
 
 
 
 
 
 
 
980c9b8
 
 
 
 
 
 
 
1a6ae0b
 
ca45ff1
b9b02f6
 
 
 
980c9b8
 
 
fe62dd7
 
 
 
980c9b8
fe62dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
980c9b8
 
 
 
1a6ae0b
 
 
 
 
 
ca45ff1
 
 
b9b02f6
 
 
 
 
 
 
 
 
 
 
 
 
 
980c9b8
 
 
b0a09b6
fe62dd7
f980128
fe62dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
347b364
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
fe62dd7
 
7e7df2a
 
 
 
 
 
 
 
 
 
fe62dd7
7e7df2a
 
 
fe62dd7
 
 
 
 
 
 
7e7df2a
 
 
 
 
401d0bc
fe62dd7
401d0bc
 
 
 
 
 
 
7e7df2a
fe62dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
401d0bc
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
401d0bc
7e7df2a
 
 
 
 
401d0bc
 
 
 
 
 
 
 
 
 
7e7df2a
401d0bc
7e7df2a
 
 
 
 
 
 
 
 
 
 
b9b02f6
 
 
 
 
 
 
 
 
 
 
7e7df2a
401d0bc
7e7df2a
 
 
 
 
 
 
 
 
 
401d0bc
7e7df2a
401d0bc
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401d0bc
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
401d0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0a09b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a762d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44b3f8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347b364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9b02f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347b364
 
 
 
 
 
 
 
f980128
 
 
 
 
 
 
 
 
 
347b364
 
 
 
 
 
 
b9b02f6
 
347b364
 
 
 
 
 
 
 
 
 
 
 
b9b02f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86b8987
b9b02f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347b364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9b02f6
 
 
 
 
347b364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a6ae0b
347b364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a6ae0b
0c76f10
 
 
 
 
 
 
c761fba
 
 
f980128
347b364
 
 
f980128
 
 
 
 
44b3f8d
 
b9b02f6
347b364
 
 
 
 
 
 
 
1a6ae0b
 
 
 
 
0c76f10
 
 
 
 
 
 
c761fba
 
 
 
 
 
347b364
 
 
 
 
 
 
 
 
 
 
 
980c9b8
 
 
 
b9b02f6
 
 
 
 
 
0c76f10
 
 
 
c761fba
 
 
 
 
1a6ae0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c76f10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347b364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
 
347b364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44b3f8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
 
 
 
347b364
 
980c9b8
 
 
 
347b364
 
0c76f10
 
 
 
 
 
 
 
 
 
 
 
 
347b364
 
 
 
 
 
 
 
980c9b8
1a6ae0b
980c9b8
 
 
 
 
1a6ae0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
980c9b8
 
1a6ae0b
980c9b8
1a6ae0b
 
b9b02f6
980c9b8
 
 
 
347b364
1a6ae0b
 
0c76f10
1a6ae0b
 
 
 
0c76f10
 
 
 
347b364
 
 
 
 
 
980c9b8
1a6ae0b
0c76f10
980c9b8
347b364
 
 
 
 
 
 
 
 
 
f980128
 
b0a09b6
f980128
 
 
347b364
 
 
 
 
 
 
 
 
 
 
b9b02f6
 
 
 
 
347b364
 
 
 
 
 
b0a09b6
347b364
f980128
980c9b8
347b364
 
1a762d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
44b3f8d
 
 
 
 
 
 
 
 
 
 
 
 
f980128
44b3f8d
 
 
 
f980128
347b364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
980c9b8
 
 
 
 
 
 
 
 
 
 
 
 
 
347b364
 
 
 
0190b5c
347b364
9e5bb0c
 
 
c761fba
 
 
 
 
 
9e5bb0c
c761fba
 
9e5bb0c
c761fba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e5bb0c
 
c761fba
9e5bb0c
c761fba
 
 
 
 
9e5bb0c
 
 
 
 
 
c761fba
 
 
 
 
 
 
9e5bb0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c761fba
9e5bb0c
 
 
 
 
c761fba
 
 
 
 
 
 
 
 
 
 
 
 
 
9e5bb0c
 
 
 
c761fba
9e5bb0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c761fba
9e5bb0c
 
 
 
 
c761fba
9e5bb0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c761fba
9e5bb0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347b364
0c76f10
347b364
 
 
 
 
 
 
 
 
 
0c76f10
347b364
 
f980128
 
 
 
 
347b364
0c76f10
 
 
 
 
 
 
 
347b364
 
0c76f10
5148a3e
 
0190b5c
5148a3e
 
 
 
 
 
 
 
 
0190b5c
 
 
 
0c76f10
5148a3e
347b364
0190b5c
347b364
 
0c76f10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347b364
0c76f10
 
347b364
1a6ae0b
 
0c76f10
1a6ae0b
 
 
 
0c76f10
 
 
 
347b364
 
 
980c9b8
1a6ae0b
0c76f10
980c9b8
347b364
 
0c76f10
347b364
 
 
 
 
 
 
 
 
 
f980128
 
b0a09b6
f980128
 
 
347b364
 
 
 
 
 
 
 
 
 
 
b9b02f6
 
 
 
 
0190b5c
347b364
 
 
 
 
 
b0a09b6
347b364
f980128
980c9b8
347b364
 
980c9b8
 
 
 
 
 
 
 
 
 
 
 
 
 
347b364
 
9e5bb0c
 
 
1a762d7
 
f980128
 
 
 
 
 
 
1a762d7
 
 
 
 
 
 
 
 
44b3f8d
 
 
 
 
 
 
 
 
 
 
 
 
1a762d7
 
 
 
44b3f8d
f980128
b9b02f6
 
 
 
 
44b3f8d
 
 
b9b02f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a762d7
 
 
 
 
 
 
 
 
 
b9b02f6
44b3f8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9b02f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f980128
b9b02f6
 
 
 
f980128
b9b02f6
 
f980128
 
 
 
 
 
347b364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c761fba
 
 
347b364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86b8987
347b364
 
9e5bb0c
f980128
 
 
 
 
9e5bb0c
 
 
 
 
 
 
 
 
 
 
 
 
347b364
9e5bb0c
 
 
 
 
 
 
 
 
 
 
347b364
 
 
 
 
 
 
 
 
 
 
9e5bb0c
 
 
 
 
347b364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e5bb0c
347b364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e5bb0c
 
 
347b364
 
 
 
 
 
 
 
 
 
 
 
 
9e5bb0c
347b364
 
 
 
 
 
 
 
 
 
 
 
 
9e5bb0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347b364
 
 
 
 
9e5bb0c
347b364
 
 
 
7e7df2a
 
 
 
fe62dd7
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
401d0bc
7e7df2a
f980128
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c854ffa
f980128
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
401d0bc
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
7e7df2a
 
 
f980128
 
b0a09b6
f980128
 
 
fe62dd7
 
7e7df2a
 
 
 
 
 
b0a09b6
fe62dd7
f980128
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
7e7df2a
 
c854ffa
 
 
 
 
 
7e7df2a
 
 
 
 
f980128
 
b0a09b6
f980128
 
 
fe62dd7
 
7e7df2a
 
 
 
 
 
 
b0a09b6
fe62dd7
f980128
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
7e7df2a
 
 
 
 
 
 
 
 
 
fe62dd7
7e7df2a
 
 
 
401d0bc
7e7df2a
fe62dd7
 
7e7df2a
 
 
 
 
 
 
 
 
 
fe62dd7
 
 
 
 
 
 
7e7df2a
fe62dd7
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe62dd7
 
 
 
7e7df2a
 
 
 
fe62dd7
 
 
 
 
7e7df2a
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
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
"""Fail-closed quality runtime helpers for the BlueMagpie-TTS Space.

The module deliberately has no import-time model downloads.  Breeze ASR 25 and
ECAPA are supplied through small injectable boundaries so the online Space can
load the real models lazily while unit tests remain deterministic and offline.
"""

from __future__ import annotations

import hashlib
import inspect
import json
import math
import operator
import threading
from dataclasses import dataclass, replace
from math import gcd
from pathlib import Path
from typing import Any, Callable, Sequence

import numpy as np
import torch
import torch.nn.functional as torch_functional

from latency_timing import timed_latency_stage
from production import (
    AsrComparison,
    CandidateSequenceSelection,
    compare_asr_text,
    count_speech_units,
)


BREEZE25_MODEL_ID = "MediaTek-Research/Breeze-ASR-25"
BREEZE25_REVISION = "cffe7ccb404d025296a00758d0a33468bec3a9d0"
BREEZE25_WEIGHT_SHA256 = (
    "c5d952b3bc03ea277209aff0ef5b5c4c055d74449ff794c02d8f4e315fdef6b6"
)
BREEZE25_ATTENTION_IMPLEMENTATION = "eager"
BREEZE25_RETURN_ATTENTION_MASK = True
BREEZE25_LANGUAGE = "zh"
BREEZE25_TASK = "transcribe"
BREEZE25_SAMPLE_RATE = 16_000
BREEZE25_MAX_SEGMENT_SECONDS = 28.0
BREEZE25_HARD_MAX_SEGMENT_SECONDS = 30.0
BREEZE25_MIN_SEGMENT_SECONDS = 1.25
BREEZE25_MIN_PAUSE_SECONDS = 0.25
BREEZE25_MAX_VERIFICATION_SEGMENTS = 12
GLYPH_HYBRID_MAX_VERIFICATION_SEGMENTS = 160
BREEZE25_MAX_MICROBATCH_SEGMENTS = 6

# Compatibility names remain available while app/evaluator callers migrate.
# They deliberately resolve to the one pinned Breeze ASR 25 contract and never
# refer to the retired OpenAI verifier models.
WHISPER_MODEL_ID = BREEZE25_MODEL_ID
WHISPER_REVISION = BREEZE25_REVISION
VERIFICATION_WHISPER_MODEL_ID = BREEZE25_MODEL_ID
VERIFICATION_WHISPER_REVISION = BREEZE25_REVISION
WHISPER_ATTENTION_IMPLEMENTATION = BREEZE25_ATTENTION_IMPLEMENTATION
WHISPER_RETURN_ATTENTION_MASK = BREEZE25_RETURN_ATTENTION_MASK
WHISPER_SAMPLE_RATE = BREEZE25_SAMPLE_RATE
WHISPER_MAX_SEGMENT_SECONDS = BREEZE25_MAX_SEGMENT_SECONDS
WHISPER_HARD_MAX_SEGMENT_SECONDS = BREEZE25_HARD_MAX_SEGMENT_SECONDS
WHISPER_MIN_SEGMENT_SECONDS = BREEZE25_MIN_SEGMENT_SECONDS
WHISPER_MIN_PAUSE_SECONDS = BREEZE25_MIN_PAUSE_SECONDS
WHISPER_MAX_VERIFICATION_SEGMENTS = BREEZE25_MAX_VERIFICATION_SEGMENTS
WHISPER_MAX_MICROBATCH_SEGMENTS = BREEZE25_MAX_MICROBATCH_SEGMENTS
SQUIM_OBJECTIVE_ASSET_PATH = "models/squim_objective_dns2020.pth"
SQUIM_OBJECTIVE_WEIGHT_SHA256 = (
    "2c54586fea83fb5eb5394d710038ee89f55cab7011a5bf730bebed4c8777e828"
)
SQUIM_OBJECTIVE_SAMPLE_RATE = 16_000
SQUIM_OBJECTIVE_MAX_WEIGHT_BYTES = 67_108_864
SQUIM_OBJECTIVE_WINDOW_SECONDS = 10.0
SQUIM_OBJECTIVE_MAX_WINDOWS = 3
ADAPTIVE_CASCADE_STAGE_LIMITS = (1, 5, 10, 15, 20, 24, 28, 32)
REQUEST_SEED_LIMIT = 2_147_483_648
ACTIVE_VOICE_TOP_DB = 35.0
ACTIVE_VOICE_FRAME_MS = 25.0
ACTIVE_VOICE_HOP_MS = 10.0
ACTIVE_VOICE_MIN_RMS = 1.0e-4
RELEASE_SPEAKER_TRIGGER_SECONDS = 1.48
SEQUENCE_FALLBACK_MAX_LOCAL_BOUNDARY_SPEAKER_DROP = 0.15
SEQUENCE_FALLBACK_SPEAKER_WEIGHT = 0.05
SEQUENCE_FALLBACK_BOUNDARY_WEIGHT = 0.10
ENDPOINT_TAIL_WINDOW_MS = 5.0
ENDPOINT_HARD_STOP_PENALTY = 0.05
ENDPOINT_ENERGY_WEIGHT = 0.02
ENDPOINT_PREFERRED_SQUIM_STOI_SLACK = 0.02
ENDPOINT_PREFERRED_SQUIM_PESQ_SLACK = 0.03
CASCADE_EVIDENCE_SCHEMA_VERSION = 6
CASCADE_EVIDENCE_LOG_PREFIX = "[BlueMagpie] cascade evidence "
CASCADE_EVIDENCE_MAX_ATTEMPTS = ADAPTIVE_CASCADE_STAGE_LIMITS[-1]
CASCADE_EVIDENCE_MAX_LOCAL_RESULTS = ADAPTIVE_CASCADE_STAGE_LIMITS[-1]
CASCADE_EVIDENCE_MAX_REASONS = 8
CASCADE_EVIDENCE_MAX_SEQUENCE_PATHS = 3
CASCADE_EVIDENCE_MAX_TEXT_UNITS = 800

_CASCADE_EVIDENCE_OUTCOMES = frozenset(
    {"returned", "no_qualified_candidate", "final_output_rejected"}
)
_CASCADE_EVIDENCE_SELECTION_MODES = frozenset(
    {"whole_trajectory", "sequence_dp", "coverage_sequence_dp"}
)
_CFG_FLOOR_REASONS = frozenset({"network", "short_text"})
_GENERATION_STOP_REASONS = frozenset(
    {"hard_stop", "native_stop", "stop_threshold"}
)
_CASCADE_EVIDENCE_REJECTION_CODES = frozenset(
    {
        "boundary_speaker_drop",
        "empty_trajectory",
        "invalid_audio_duration",
        "invalid_chunk_artifacts",
        "invalid_gate_config",
        "invalid_joined_verification",
        "malformed_observation",
        "missing_pace_evidence",
        "missing_squim_evidence",
        "missing_speaker_evidence",
        "network_protected_span_mismatch",
        "nonfinite_score",
        "nonfinite_trajectory_score",
        "pace_too_fast",
        "semantic_gate",
        "speaker_similarity",
        "squim_pesq_too_low",
        "squim_stoi_too_low",
        "truncated",
    }
)
_CANDIDATE_GATE_OVERRIDE_KEYS = frozenset(
    {
        "max_prefix_cer",
        "max_suffix_cer",
    }
)


@dataclass(frozen=True)
class GenerationPolicy:
    """Candidate-specific endpoint duration estimate used by the Space."""

    name: str
    cjk_cps: float
    ascii_cps: float
    hard_stop_margin_steps: int
    short_headroom_max_units: int = 0
    short_hard_stop_floor_steps: int = 0


@dataclass(frozen=True)
class CandidateGenerationContext:
    """Immutable global identity and row-local schedule for one generation.

    ``candidate_index`` and ``seed`` retain the request-global attempt identity.
    ``chunk_candidate_ordinals`` is independent: zero denotes the initial
    trajectory and positive values count refill attempts within each source
    chunk.  Context-aware callbacks can therefore rotate generation policies
    per chunk without changing the canonical seed schedule.
    """

    candidate_index: int
    seed: int
    chunk_indices: tuple[int, ...]
    chunk_candidate_ordinals: tuple[int, ...]


BASE_GENERATION_POLICY = GenerationPolicy(
    name="base",
    cjk_cps=5.2,
    ascii_cps=4.6,
    hard_stop_margin_steps=1,
)
SAFE_DURATION_GENERATION_POLICY = GenerationPolicy(
    name="safe_duration",
    cjk_cps=4.6,
    ascii_cps=4.0,
    hard_stop_margin_steps=1,
)
COMPLETION_HEADROOM_GENERATION_POLICY = GenerationPolicy(
    name="completion_headroom",
    cjk_cps=4.2,
    ascii_cps=3.6,
    hard_stop_margin_steps=1,
    short_headroom_max_units=2,
    short_hard_stop_floor_steps=5,
)
MIXED_CFG_SCHEDULE = "row_ordinal_zero_and_even_primary_odd_alternate"
MIXED_CFG_PRIMARY = 3.0
MIXED_CFG_ALTERNATE = 2.0
MIXED_CFG_SHORT_TEXT_MAX_UNITS = 6
MIXED_CFG_SHORT_TEXT_MIN = 3.0
MIXED_CFG_NETWORK_MIN = 3.0


def generation_policy_for_candidate_offset(candidate_offset: int) -> GenerationPolicy:
    """Map offsets to base, safe, and sparse completion-headroom estimates.

    The policy changes only the native-duration endpoint estimate.  It is not a
    minimum-length policy and therefore never holds the generation loop open to
    enforce playback pace. Every fourth retry receives modest completion
    headroom; the adaptive stop decision and all semantic/tail gates remain
    unchanged.
    """

    if isinstance(candidate_offset, (bool, np.bool_)):
        raise ValueError("candidate_offset must be a non-negative integer")
    try:
        offset = operator.index(candidate_offset)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("candidate_offset must be a non-negative integer") from error
    if offset < 0:
        raise ValueError("candidate_offset must be a non-negative integer")
    if offset == 0:
        return BASE_GENERATION_POLICY
    if offset % 4 == 0:
        return COMPLETION_HEADROOM_GENERATION_POLICY
    return SAFE_DURATION_GENERATION_POLICY


def generation_cfg_for_candidate_offset(
    candidate_offset: int,
    *,
    primary_cfg: float = MIXED_CFG_PRIMARY,
    alternate_cfg: float = MIXED_CFG_ALTERNATE,
) -> float:
    """Return the frozen interleaved CFG for one candidate offset.

    Offset zero and every positive even offset use the primary CFG. Positive
    odd offsets use the alternate CFG. This preserves the candidate seeds,
    endpoint policies, and 32-generation budget while adding the independently
    observed CFG diversity; no unvalidated schedule is exposed at runtime.
    """

    if isinstance(candidate_offset, (bool, np.bool_)):
        raise ValueError("candidate_offset must be a non-negative integer")
    try:
        offset = operator.index(candidate_offset)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("candidate_offset must be a non-negative integer") from error
    if offset < 0:
        raise ValueError("candidate_offset must be a non-negative integer")
    try:
        primary = float(primary_cfg)
        alternate = float(alternate_cfg)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("mixed CFG values must be finite values in [1.0, 4.0]") from error
    if not all(
        math.isfinite(value) and 1.0 <= value <= 4.0
        for value in (primary, alternate)
    ):
        raise ValueError("mixed CFG values must be finite values in [1.0, 4.0]")
    return primary if offset % 2 == 0 else alternate


def resolve_request_seed(
    request_seed: int | None,
    random_seed_factory: Callable[[int], int],
) -> int:
    """Return a validated root seed, drawing randomness only for ``None``."""

    candidate = (
        random_seed_factory(REQUEST_SEED_LIMIT)
        if request_seed is None
        else request_seed
    )
    if isinstance(candidate, (bool, np.bool_)):
        raise ValueError(f"request_seed must be an integer in [0, {REQUEST_SEED_LIMIT})")
    try:
        # ``operator.index`` semantics reject floats and numeric strings while
        # accepting Python and NumPy integer scalars.
        seed = operator.index(candidate)
    except (AttributeError, TypeError, ValueError, OverflowError) as error:
        raise ValueError(
            f"request_seed must be an integer in [0, {REQUEST_SEED_LIMIT})"
        ) from error
    seed = int(seed)
    if not 0 <= seed < REQUEST_SEED_LIMIT:
        raise ValueError(f"request_seed must be an integer in [0, {REQUEST_SEED_LIMIT})")
    return seed


def _finite_float(
    value: Any,
    *,
    minimum: float | None = None,
    maximum: float | None = None,
) -> float | None:
    if isinstance(value, (bool, np.bool_)):
        return None
    try:
        result = float(value)
    except (TypeError, ValueError, OverflowError):
        return None
    if not math.isfinite(result):
        return None
    if minimum is not None and result < minimum:
        return None
    if maximum is not None and result > maximum:
        return None
    return result


def release_speaker_measurement_required(active_duration_seconds: float) -> bool:
    """Trigger online ECAPA slightly before the external 1.50 s hard gate.

    The 20 ms margin covers a bounded RMS-frame shift caused by WAV
    serialization while leaving the published external eligibility threshold
    unchanged.
    """

    duration = _finite_float(active_duration_seconds, minimum=0.0)
    return duration is not None and duration >= RELEASE_SPEAKER_TRIGGER_SECONDS


def _mono_audio(audio: np.ndarray | Sequence[float]) -> np.ndarray:
    """Return contiguous mono float32 audio, rejecting ambiguous/bad inputs."""

    waveform = np.asarray(audio)
    if waveform.ndim == 1:
        pass
    elif waveform.ndim == 2:
        first, second = waveform.shape
        if first <= 8 and second > first:
            waveform = waveform.mean(axis=0)
        elif second <= 8 and first > second:
            waveform = waveform.mean(axis=1)
        else:
            raise ValueError("2-D audio must have an identifiable channel axis (at most 8 channels)")
    else:
        raise ValueError("audio must be a one- or two-dimensional array")
    waveform = np.asarray(waveform, dtype=np.float32).reshape(-1)
    if waveform.size == 0:
        raise ValueError("audio is empty")
    if not np.isfinite(waveform).all():
        raise ValueError("audio contains non-finite samples")
    return np.ascontiguousarray(waveform)


def _resample_audio(audio: np.ndarray, sample_rate: int, target_rate: int) -> np.ndarray:
    try:
        source_rate = int(sample_rate)
        destination_rate = int(target_rate)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("sample rates must be positive integers") from error
    if source_rate <= 0 or destination_rate <= 0:
        raise ValueError("sample rates must be positive integers")
    if source_rate == destination_rate:
        return np.ascontiguousarray(audio, dtype=np.float32)

    # SpeechBrain already depends on SciPy.  ``resample_poly`` avoids an
    # undeclared optional ``librosa`` resampler dependency in the Space image.
    from scipy.signal import resample_poly

    common_divisor = gcd(source_rate, destination_rate)
    output = resample_poly(
        np.asarray(audio, dtype=np.float32),
        destination_rate // common_divisor,
        source_rate // common_divisor,
    )
    output = np.asarray(output, dtype=np.float32).reshape(-1)
    if output.size == 0 or not np.isfinite(output).all():
        raise ValueError("resampling produced invalid audio")
    return np.ascontiguousarray(output)


@dataclass(frozen=True)
class SquimObjectiveRuntime:
    """Pinned CPU SQUIM objective model and its verified weight identity."""

    model: Any
    device: torch.device
    sample_rate: int
    weight_sha256: str


@dataclass(frozen=True)
class SquimObjectiveEvidence:
    """Finite no-reference acoustic metrics for one bounded waveform view."""

    stoi: float
    pesq: float
    si_sdr: float
    window_count: int


def _bounded_file_sha256(path: str | Path) -> str:
    selected = Path(path)
    try:
        size = selected.stat().st_size
    except OSError as error:
        raise RuntimeError("pinned SQUIM weight is unavailable") from error
    if not selected.is_file() or not 0 < size <= SQUIM_OBJECTIVE_MAX_WEIGHT_BYTES:
        raise RuntimeError("pinned SQUIM weight has an invalid size")
    digest = hashlib.sha256()
    total = 0
    try:
        with selected.open("rb") as handle:
            while True:
                block = handle.read(1024 * 1024)
                if not block:
                    break
                total += len(block)
                if total > SQUIM_OBJECTIVE_MAX_WEIGHT_BYTES:
                    raise RuntimeError("pinned SQUIM weight exceeds the size limit")
                digest.update(block)
    except OSError as error:
        raise RuntimeError("pinned SQUIM weight cannot be read") from error
    if total != size:
        raise RuntimeError("pinned SQUIM weight changed while hashing")
    return digest.hexdigest()


def load_pinned_squim_objective_runtime(
    *,
    asset_fetcher: Callable[[str], str | Path] | None = None,
    model_factory: Callable[[], Any] | None = None,
    state_loader: Callable[..., Any] | None = None,
    file_hasher: Callable[[str | Path], str] | None = None,
) -> SquimObjectiveRuntime:
    """Load SQUIM on CPU only after exact full-file SHA-256 verification."""

    import torchaudio

    fetcher = asset_fetcher or torchaudio.utils._download_asset
    factory = model_factory or torchaudio.models.squim_objective_base
    loader = state_loader or torch.load
    hasher = file_hasher or _bounded_file_sha256
    try:
        weight_path = fetcher(SQUIM_OBJECTIVE_ASSET_PATH)
    except Exception as error:
        raise RuntimeError("pinned SQUIM weight download failed") from error
    try:
        digest = str(hasher(weight_path)).casefold()
    except RuntimeError:
        raise
    except Exception as error:
        raise RuntimeError("pinned SQUIM weight hash failed") from error
    if digest != SQUIM_OBJECTIVE_WEIGHT_SHA256:
        raise RuntimeError("pinned SQUIM weight SHA-256 mismatch")

    # The hash check deliberately precedes deserialization.  ``weights_only``
    # further constrains the trusted, pinned state-dict load boundary.
    try:
        state_dict = loader(weight_path, map_location="cpu", weights_only=True)
        model = factory()
        model.load_state_dict(state_dict, strict=True)
        model = model.to(torch.device("cpu"))
        model.eval()
    except Exception as error:
        raise RuntimeError("pinned SQUIM model initialization failed") from error
    return SquimObjectiveRuntime(
        model=model,
        device=torch.device("cpu"),
        sample_rate=SQUIM_OBJECTIVE_SAMPLE_RATE,
        weight_sha256=SQUIM_OBJECTIVE_WEIGHT_SHA256,
    )


class LazySquimObjective:
    """Thread-safe lazy CPU loader for the pinned no-reference metric model."""

    def __init__(
        self,
        runtime_loader: Callable[[], SquimObjectiveRuntime] | None = None,
    ) -> None:
        self._runtime_loader = runtime_loader or load_pinned_squim_objective_runtime
        self._runtime: SquimObjectiveRuntime | None = None
        self._load_lock = threading.Lock()
        self._inference_lock = threading.Lock()

    def get_runtime(self) -> SquimObjectiveRuntime:
        runtime = self._runtime
        if runtime is not None:
            return runtime
        with self._load_lock:
            if self._runtime is None:
                loaded = self._runtime_loader()
                if not isinstance(loaded, SquimObjectiveRuntime):
                    raise RuntimeError("SQUIM loader returned an invalid runtime")
                if (
                    loaded.device != torch.device("cpu")
                    or loaded.sample_rate != SQUIM_OBJECTIVE_SAMPLE_RATE
                    or loaded.weight_sha256 != SQUIM_OBJECTIVE_WEIGHT_SHA256
                ):
                    raise RuntimeError("SQUIM runtime violates the pinned contract")
                self._runtime = loaded
            return self._runtime

    @property
    def inference_lock(self) -> threading.Lock:
        return self._inference_lock


_DEFAULT_SQUIM_OBJECTIVE = LazySquimObjective()


def _bounded_squim_windows(waveform: np.ndarray) -> tuple[np.ndarray, ...]:
    window_samples = int(
        round(SQUIM_OBJECTIVE_WINDOW_SECONDS * SQUIM_OBJECTIVE_SAMPLE_RATE)
    )
    if waveform.size <= window_samples:
        return (np.ascontiguousarray(waveform, dtype=np.float32),)
    starts = np.linspace(
        0,
        waveform.size - window_samples,
        num=SQUIM_OBJECTIVE_MAX_WINDOWS,
    ).round().astype(int)
    windows = tuple(
        np.ascontiguousarray(
            waveform[start : start + window_samples],
            dtype=np.float32,
        )
        for start in dict.fromkeys(starts.tolist())
    )
    if not windows or len(windows) > SQUIM_OBJECTIVE_MAX_WINDOWS:
        raise ValueError("SQUIM window selection failed")
    return windows


@torch.inference_mode()
@timed_latency_stage("squim")
def squim_objective_evidence_from_audio(
    audio: np.ndarray | Sequence[float],
    sample_rate: int,
    *,
    lazy_runtime: LazySquimObjective | None = None,
    runtime: SquimObjectiveRuntime | None = None,
) -> SquimObjectiveEvidence:
    """Measure bounded SQUIM evidence with conservative long-audio pooling."""

    if runtime is not None and lazy_runtime is not None:
        raise ValueError("pass either runtime or lazy_runtime, not both")
    waveform = _mono_audio(audio)
    waveform = _resample_audio(
        waveform,
        int(sample_rate),
        SQUIM_OBJECTIVE_SAMPLE_RATE,
    )
    windows = _bounded_squim_windows(waveform)
    selected_lazy = lazy_runtime or _DEFAULT_SQUIM_OBJECTIVE
    selected_runtime = runtime or selected_lazy.get_runtime()
    if (
        not isinstance(selected_runtime, SquimObjectiveRuntime)
        or selected_runtime.device != torch.device("cpu")
        or selected_runtime.sample_rate != SQUIM_OBJECTIVE_SAMPLE_RATE
        or selected_runtime.weight_sha256 != SQUIM_OBJECTIVE_WEIGHT_SHA256
    ):
        raise RuntimeError("SQUIM runtime violates the pinned contract")
    tensor = torch.from_numpy(np.stack(windows)).to(
        selected_runtime.device,
        dtype=torch.float32,
    )
    lock = selected_lazy.inference_lock if runtime is None else threading.Lock()
    try:
        with lock:
            outputs = selected_runtime.model(tensor)
    except Exception as error:
        raise RuntimeError("pinned SQUIM inference failed") from error
    if not isinstance(outputs, (tuple, list)) or len(outputs) != 3:
        raise RuntimeError("pinned SQUIM returned an invalid result")
    rows: list[np.ndarray] = []
    for output in outputs:
        values = torch.as_tensor(output).detach().float().cpu().numpy().reshape(-1)
        if values.size != len(windows) or not np.isfinite(values).all():
            raise ValueError("SQUIM evidence is missing or non-finite")
        rows.append(values.astype(np.float64, copy=False))
    stoi_values, pesq_values, si_sdr_values = rows
    return SquimObjectiveEvidence(
        # A single degraded long-form window must not be hidden by clean prose.
        stoi=float(np.min(stoi_values)),
        pesq=float(np.min(pesq_values)),
        si_sdr=float(np.median(si_sdr_values)),
        window_count=len(windows),
    )


def _trim_active_speech(
    audio: np.ndarray,
    *,
    top_db: float = 35.0,
    frame_length: int = 512,
    hop_length: int = 128,
) -> np.ndarray:
    threshold_db = _finite_float(top_db, minimum=0.0)
    if threshold_db is None:
        raise ValueError("top_db must be finite and non-negative")
    if float(np.max(np.abs(audio))) <= 1.0e-7:
        raise ValueError("audio contains no active speech")

    import librosa

    intervals = librosa.effects.split(
        audio,
        top_db=threshold_db,
        frame_length=max(32, int(frame_length)),
        hop_length=max(1, int(hop_length)),
    )
    if intervals.size == 0:
        raise ValueError("audio contains no active speech")
    start = int(intervals[0, 0])
    stop = int(intervals[-1, 1])
    active = np.asarray(audio[start:stop], dtype=np.float32)
    if active.size == 0 or float(np.max(np.abs(active))) <= 1.0e-7:
        raise ValueError("audio contains no active speech")
    return np.ascontiguousarray(active)


def trim_release_speaker_activity(
    audio: np.ndarray | Sequence[float],
    sample_rate: int,
    *,
    top_db: float = 35.0,
    margin_seconds: float = 0.05,
) -> np.ndarray:
    """Trim speaker audio with the independent release-gate contract.

    This deliberately mirrors ``evaluate_space_profile.trim_speaker_activity``:
    25 ms RMS frames, 10 ms hop, peak-minus-35 dB with a 1e-4 floor, and a
    50 ms outer margin.  It remains separate from the interval-union duration
    used for pace and speaker eligibility.
    """

    signal = _mono_audio(audio)
    try:
        rate = operator.index(sample_rate)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("sample rate must be positive") from error
    relative_db = _finite_float(top_db, minimum=0.0)
    margin_duration = _finite_float(margin_seconds, minimum=0.0)
    if rate <= 0 or relative_db is None or margin_duration is None:
        raise ValueError("release speaker trim settings are invalid")
    frame = max(160, int(round(0.025 * rate)))
    hop = max(80, int(round(0.010 * rate)))
    if signal.size < frame:
        active = signal
    else:
        starts = np.arange(0, signal.size - frame + 1, hop, dtype=np.int64)
        rms = np.asarray(
            [
                float(
                    np.sqrt(
                        np.mean(
                            np.square(signal[start : start + frame], dtype=np.float64)
                        )
                    )
                )
                for start in starts
            ],
            dtype=np.float64,
        )
        peak = float(rms.max(initial=0.0))
        if peak <= 0.0:
            active = signal
        else:
            threshold = max(1.0e-4, peak * (10.0 ** (-relative_db / 20.0)))
            active_frames = np.flatnonzero(rms >= threshold)
            if active_frames.size == 0:
                active = signal
            else:
                margin = max(0, int(round(margin_duration * rate)))
                begin = max(0, int(starts[int(active_frames[0])]) - margin)
                end = min(
                    signal.size,
                    int(starts[int(active_frames[-1])]) + frame + margin,
                )
                active = signal[begin:end] if end > begin else signal
    active = np.ascontiguousarray(active, dtype=np.float32)
    if active.size == 0 or float(np.max(np.abs(active))) <= 1.0e-7:
        raise ValueError("audio contains no active speech")
    return active


def active_voiced_intervals(
    audio: np.ndarray | Sequence[float],
    sample_rate: int,
    *,
    top_db: float = ACTIVE_VOICE_TOP_DB,
    frame_ms: float = ACTIVE_VOICE_FRAME_MS,
    hop_ms: float = ACTIVE_VOICE_HOP_MS,
    min_rms: float = ACTIVE_VOICE_MIN_RMS,
) -> tuple[tuple[int, int], ...]:
    """Return the deterministic union of active RMS-frame intervals.

    This is the same 25 ms / 10 ms, peak-minus-35 dB, 1e-4 floor contract
    used by the independent hosted evaluator.  Unlike first-to-last trimming,
    the interval union excludes internal punctuation and joining pauses from
    both online pace evidence and the speaker-gate duration threshold.
    """

    signal = _mono_audio(audio)
    if isinstance(sample_rate, (bool, np.bool_)):
        raise ValueError("sample rate must be positive")
    try:
        source_rate = operator.index(sample_rate)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("sample rate must be positive") from error
    if source_rate <= 0:
        raise ValueError("sample rate must be positive")
    frame_duration = _finite_float(frame_ms, minimum=0.0)
    hop_duration = _finite_float(hop_ms, minimum=0.0)
    rms_floor = _finite_float(min_rms, minimum=0.0)
    relative_db = _finite_float(top_db, minimum=0.0)
    if (
        frame_duration is None
        or frame_duration <= 0.0
        or hop_duration is None
        or hop_duration <= 0.0
        or rms_floor is None
        or rms_floor <= 0.0
        or relative_db is None
    ):
        raise ValueError("active-voice detector settings are invalid")

    frame = max(1, int(round(frame_duration * source_rate / 1000.0)))
    hop = max(1, int(round(hop_duration * source_rate / 1000.0)))
    if signal.size <= frame:
        starts = np.asarray([0], dtype=np.int64)
    else:
        starts = np.arange(0, signal.size - frame + 1, hop, dtype=np.int64)
        final_start = signal.size - frame
        if int(starts[-1]) != final_start:
            starts = np.append(starts, final_start)
    rms = np.asarray(
        [
            float(
                np.sqrt(
                    np.mean(
                        np.square(
                            signal[int(start) : int(start) + frame],
                            dtype=np.float64,
                        )
                    )
                )
            )
            for start in starts
        ],
        dtype=np.float64,
    )
    peak = float(rms.max(initial=0.0))
    threshold = max(rms_floor, peak * 10.0 ** (-relative_db / 20.0))
    active_starts = starts[rms >= threshold]
    intervals: list[list[int]] = []
    for raw_start in active_starts:
        start = int(raw_start)
        end = min(signal.size, start + frame)
        if intervals and start <= intervals[-1][1]:
            intervals[-1][1] = max(intervals[-1][1], end)
        else:
            intervals.append([start, end])
    return tuple((start, end) for start, end in intervals)


def active_voiced_duration_seconds(
    audio: np.ndarray | Sequence[float],
    sample_rate: int,
    **detector_kwargs: Any,
) -> float:
    """Measure active interval-union duration under the hosted gate contract."""

    intervals = active_voiced_intervals(audio, sample_rate, **detector_kwargs)
    active_samples = sum(end - start for start, end in intervals)
    return active_samples / float(operator.index(sample_rate))


@dataclass(frozen=True)
class Breeze25Runtime:
    """Loaded processor/model pair for deterministic Breeze ASR 25."""

    processor: Any
    model: Any
    device: torch.device
    dtype: torch.dtype


def _load_breeze25_runtime(
    model_id: str,
    revision: str,
    *,
    device: str | torch.device | None = None,
    processor_factory: Any | None = None,
    model_factory: Any | None = None,
) -> Breeze25Runtime:
    """Load one exact ASR revision through the shared deterministic contract.

    Factory injection exists for offline tests.  The default imports
    ``transformers`` only when this function is first called.
    """

    if processor_factory is None or model_factory is None:
        from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor

        processor_factory = processor_factory or AutoProcessor
        model_factory = model_factory or AutoModelForSpeechSeq2Seq

    selected_device = torch.device(
        device if device is not None else ("cuda" if torch.cuda.is_available() else "cpu")
    )
    dtype = torch.float16 if selected_device.type == "cuda" else torch.float32
    processor = processor_factory.from_pretrained(
        model_id,
        revision=revision,
    )
    model = model_factory.from_pretrained(
        model_id,
        revision=revision,
        attn_implementation=BREEZE25_ATTENTION_IMPLEMENTATION,
        torch_dtype=dtype,
        low_cpu_mem_usage=True,
        use_safetensors=True,
    )
    model = model.to(selected_device)
    model.eval()
    return Breeze25Runtime(
        processor=processor,
        model=model,
        device=selected_device,
        dtype=dtype,
    )


def load_pinned_breeze25_runtime(
    *,
    device: str | torch.device | None = None,
    processor_factory: Any | None = None,
    model_factory: Any | None = None,
) -> Breeze25Runtime:
    """Load the sole pinned content verifier used by every semantic gate."""

    return _load_breeze25_runtime(
        BREEZE25_MODEL_ID,
        BREEZE25_REVISION,
        device=device,
        processor_factory=processor_factory,
        model_factory=model_factory,
    )


class LazyBreeze25ASR:
    """Thread-safe one-shot lazy loader with an injectable runtime factory."""

    def __init__(
        self,
        runtime_loader: Callable[[], Breeze25Runtime] | None = None,
    ) -> None:
        self._runtime_loader = runtime_loader or load_pinned_breeze25_runtime
        self._runtime: Breeze25Runtime | None = None
        self._lock = threading.Lock()

    def get_runtime(self) -> Breeze25Runtime:
        runtime = self._runtime
        if runtime is not None:
            return runtime
        with self._lock:
            if self._runtime is None:
                self._runtime = self._runtime_loader()
            return self._runtime


_DEFAULT_BREEZE25 = LazyBreeze25ASR()


def _qualified_breeze25_pause_ranges(
    waveform: np.ndarray,
    *,
    sample_rate: int,
    minimum_pause_seconds: float = 0.25,
) -> tuple[tuple[int, int], ...]:
    """Find waveform-only pause atoms with active evidence on both sides.

    A robust percentile, rather than the loudest frame, defines the local
    energy scale. Every pause also needs active evidence on both sides, so a
    silent waveform or one loud transient cannot manufacture pause atoms.
    """

    rate = int(sample_rate)
    if rate <= 0:
        raise ValueError("sample_rate must be positive")
    probe_radius = max(1, int(round(0.01 * rate)))
    probe_hop = max(1, int(round(0.01 * rate)))
    if waveform.size < 2 * probe_radius:
        return ()
    probes = np.arange(
        probe_radius,
        waveform.size - probe_radius + 1,
        probe_hop,
        dtype=np.int64,
    )
    if probes.size == 0:
        return ()

    squared = np.square(waveform, dtype=np.float64)
    integral = np.concatenate((np.zeros(1, dtype=np.float64), np.cumsum(squared)))
    window_energy = integral[probes + probe_radius] - integral[probes - probe_radius]
    rms = np.sqrt(window_energy / float(2 * probe_radius))
    robust_index = max(0, min(rms.size - 1, int(math.ceil(0.90 * rms.size)) - 1))
    robust_peak = float(np.partition(rms, robust_index)[robust_index])
    quiet_threshold = max(1.0e-5, robust_peak * 10.0 ** (-35.0 / 20.0))
    active_threshold = max(2.0e-5, quiet_threshold * 4.0, robust_peak * 0.25)
    quiet_positions = probes[rms <= quiet_threshold]

    quiet_runs: list[tuple[int, int]] = []
    for position in quiet_positions.tolist():
        if quiet_runs and position <= quiet_runs[-1][1] + probe_hop:
            quiet_runs[-1] = (quiet_runs[-1][0], position)
        else:
            quiet_runs.append((position, position))

    minimum_pause = max(1, int(round(minimum_pause_seconds * rate)))
    evidence_window = max(probe_hop, int(round(0.75 * rate)))
    minimum_active_frames = max(1, int(math.ceil(0.10 * rate / probe_hop)))
    qualified: list[tuple[int, int]] = []
    for begin, end in quiet_runs:
        run_start = max(0, begin - probe_radius)
        run_stop = min(waveform.size, end + probe_radius)
        if run_stop - run_start < minimum_pause:
            continue
        left_active = np.count_nonzero(
            (probes >= max(0, run_start - evidence_window))
            & (probes < run_start)
            & (rms >= active_threshold)
        )
        right_active = np.count_nonzero(
            (probes > run_stop)
            & (probes <= min(waveform.size, run_stop + evidence_window))
            & (rms >= active_threshold)
        )
        if left_active < minimum_active_frames or right_active < minimum_active_frames:
            continue
        qualified.append((run_start, run_stop))
    return tuple(qualified)


def _split_short_breeze25_audio_at_sustained_pause(
    waveform: np.ndarray,
    *,
    sample_rate: int,
    minimum_pause_seconds: float = 0.25,
    minimum_segment_seconds: float = 1.25,
) -> tuple[np.ndarray, ...]:
    """Expose genuine pause atoms as deterministic adjacent audio segments."""

    rate = int(sample_rate)
    if rate <= 0:
        raise ValueError("sample_rate must be positive")
    minimum_segment = max(1, int(round(minimum_segment_seconds * rate)))
    if waveform.size < 2 * minimum_segment:
        return (waveform,)
    pause_ranges = _qualified_breeze25_pause_ranges(
        waveform,
        sample_rate=rate,
        minimum_pause_seconds=minimum_pause_seconds,
    )

    boundaries = [0]
    for begin, end in pause_ranges:
        boundary = (begin + end) // 2
        if (
            boundary - boundaries[-1] >= minimum_segment
            and waveform.size - boundary >= minimum_segment
        ):
            boundaries.append(boundary)
    boundaries.append(waveform.size)
    return tuple(
        np.ascontiguousarray(waveform[start:stop], dtype=np.float32)
        for start, stop in zip(boundaries, boundaries[1:])
    )


def _split_breeze25_audio(
    waveform: np.ndarray,
    *,
    sample_rate: int = BREEZE25_SAMPLE_RATE,
    max_segment_seconds: float = BREEZE25_MAX_SEGMENT_SECONDS,
    boundary_search_seconds: float = 1.5,
    max_verification_segments: int = BREEZE25_MAX_VERIFICATION_SEGMENTS,
) -> tuple[np.ndarray, ...]:
    """Create bounded verification segments without cutting voiced audio.

    Every qualified pause is retained when doing so keeps all segments below
    the target cap. This prevents a long multi-chunk decode from swallowing
    several already-separated clauses in one Breeze25 context. If the natural
    pause atoms cannot bound the contexts, the coarse latest-pause fallback is
    used; a long voiced span without such a pause still fails closed.
    """

    maximum_seconds = _finite_float(max_segment_seconds, minimum=1.0)
    search_seconds = _finite_float(boundary_search_seconds, minimum=0.0)
    if (
        type(max_verification_segments) is not int
        or not 1
        <= max_verification_segments
        <= GLYPH_HYBRID_MAX_VERIFICATION_SEGMENTS
    ):
        raise ValueError(
            "Breeze25 segment budget must be an exact integer in [1, 160]"
        )
    if maximum_seconds is None or search_seconds is None or sample_rate <= 0:
        raise ValueError("invalid Breeze25 segmentation settings")
    maximum_samples = max(1, int(round(maximum_seconds * sample_rate)))
    hard_cap_samples = int(round(BREEZE25_HARD_MAX_SEGMENT_SECONDS * sample_rate))
    if maximum_samples > hard_cap_samples:
        raise ValueError("Breeze25 target segment cap exceeds the 30-second hard limit")
    minimum_segment_samples = max(
        1,
        int(round(BREEZE25_MIN_SEGMENT_SECONDS * sample_rate)),
    )
    if waveform.size > hard_cap_samples * max_verification_segments:
        raise ValueError("verification audio exceeds the bounded ASR segment budget")

    pause_ranges = _qualified_breeze25_pause_ranges(
        waveform,
        sample_rate=sample_rate,
        minimum_pause_seconds=BREEZE25_MIN_PAUSE_SECONDS,
    )
    pause_segments = _split_short_breeze25_audio_at_sustained_pause(
        waveform,
        sample_rate=sample_rate,
        minimum_pause_seconds=BREEZE25_MIN_PAUSE_SECONDS,
        minimum_segment_seconds=BREEZE25_MIN_SEGMENT_SECONDS,
    )
    if len(pause_segments) > max_verification_segments:
        raise ValueError("verification audio exceeds the pause segment cap")
    if all(segment.size <= maximum_samples for segment in pause_segments):
        return pause_segments

    coarse_minimum_samples = min(
        maximum_samples // 2,
        max(minimum_segment_samples, int(round(6.0 * sample_rate))),
    )
    boundaries = [0]
    while waveform.size - boundaries[-1] > maximum_samples:
        segment_start = boundaries[-1]
        lower = segment_start + coarse_minimum_samples
        upper = min(waveform.size, segment_start + maximum_samples)
        boundary = None
        for pause_start, pause_stop in reversed(pause_ranges):
            clipped_start = max(lower, pause_start)
            clipped_stop = min(upper, pause_stop)
            if clipped_start > clipped_stop:
                continue
            midpoint = (pause_start + pause_stop) // 2
            candidate = min(clipped_stop, max(clipped_start, midpoint))
            boundary = candidate
            break
        if boundary is None:
            raise ValueError(
                "long verification audio has no qualified 250ms pause before the cap"
            )
        boundaries.append(boundary)
        if len(boundaries) > max_verification_segments:
            raise ValueError("verification audio exceeds the pause segment cap")
    boundaries.append(waveform.size)
    segments = tuple(
        np.ascontiguousarray(waveform[start:stop], dtype=np.float32)
        for start, stop in zip(boundaries, boundaries[1:])
    )
    if len(segments) >= 2 and segments[-1].size < minimum_segment_samples:
        merged_samples = segments[-2].size + segments[-1].size
        if merged_samples > hard_cap_samples:
            raise ValueError("terminal verifier tail cannot be merged below 30 seconds")
        segments = (
            *segments[:-2],
            np.ascontiguousarray(
                np.concatenate((segments[-2], segments[-1])),
                dtype=np.float32,
            ),
        )
    if (
        not segments
        or any(segment.size == 0 for segment in segments)
        or len(segments) > max_verification_segments
        or any(segment.size > hard_cap_samples for segment in segments)
        or (
            waveform.size >= minimum_segment_samples
            and any(segment.size < minimum_segment_samples for segment in segments)
        )
        or sum(segment.size for segment in segments) != waveform.size
    ):
        raise ValueError("failed to split audio within Breeze25's segment limit")
    return segments


@torch.inference_mode()
def _transcribe_breeze25(
    audio: np.ndarray | Sequence[float],
    sample_rate: int,
    *,
    lazy_asr: LazyBreeze25ASR | None = None,
    runtime: Breeze25Runtime | None = None,
    language: str | None = BREEZE25_LANGUAGE,
    task: str = BREEZE25_TASK,
    max_new_tokens: int = 128,
    max_verification_segments: int = BREEZE25_MAX_VERIFICATION_SEGMENTS,
) -> str:
    """Transcribe ndarray audio using the sole pinned Breeze25 decoder.

    ``runtime`` and ``lazy_asr`` are mutually exclusive injection points.  An
    empty decoded string is returned as-is; the semantic verifier will reject
    it rather than accepting an arbitrary TTS fallback. Production fixes the
    shared mixed-language embedding with ``language="zh"``. Passing ``None``
    is supported only as an explicit evaluation injection and omits the
    language argument so the model performs its own language detection.
    """

    if runtime is not None and lazy_asr is not None:
        raise ValueError("pass either runtime or lazy_asr, not both")
    token_limit = int(max_new_tokens)
    if token_limit <= 0:
        raise ValueError("max_new_tokens must be positive")
    waveform = _resample_audio(
        _mono_audio(audio),
        int(sample_rate),
        BREEZE25_SAMPLE_RATE,
    )
    segments = _split_breeze25_audio(
        waveform,
        max_verification_segments=max_verification_segments,
    )
    selected_runtime = runtime or (lazy_asr or _DEFAULT_BREEZE25).get_runtime()
    decoded_segments: list[str] = []
    for start in range(0, len(segments), BREEZE25_MAX_MICROBATCH_SEGMENTS):
        microbatch = segments[start : start + BREEZE25_MAX_MICROBATCH_SEGMENTS]
        processor_input: np.ndarray | list[np.ndarray]
        processor_input = microbatch[0] if len(microbatch) == 1 else list(microbatch)
        processor_output = selected_runtime.processor(
            processor_input,
            sampling_rate=BREEZE25_SAMPLE_RATE,
            return_tensors="pt",
            return_attention_mask=BREEZE25_RETURN_ATTENTION_MASK,
        )
        features = processor_output.input_features.to(
            device=selected_runtime.device,
            dtype=selected_runtime.dtype,
        )
        attention_mask = processor_output.attention_mask.to(
            device=selected_runtime.device
        )
        generation_kwargs = {
            "attention_mask": attention_mask,
            "task": task,
            "do_sample": False,
            "num_beams": 1,
            "max_new_tokens": token_limit,
        }
        if language is not None:
            generation_kwargs["language"] = language
        token_ids = selected_runtime.model.generate(features, **generation_kwargs)
        decoded = selected_runtime.processor.batch_decode(
            token_ids,
            skip_special_tokens=True,
        )
        if not decoded or len(decoded) != len(microbatch):
            return ""
        decoded_segments.extend(str(text).strip() for text in decoded)
    return " ".join(text for text in decoded_segments if text)


@timed_latency_stage("breeze25_asr")
def transcribe_breeze25(
    audio: np.ndarray | Sequence[float],
    sample_rate: int,
    *,
    lazy_asr: LazyBreeze25ASR | None = None,
    runtime: Breeze25Runtime | None = None,
    language: str | None = BREEZE25_LANGUAGE,
    task: str = BREEZE25_TASK,
    max_new_tokens: int = 128,
    max_verification_segments: int = BREEZE25_MAX_VERIFICATION_SEGMENTS,
) -> str:
    """Transcribe with the sole pinned Breeze ASR 25 content verifier."""

    return _transcribe_breeze25(
        audio,
        sample_rate,
        lazy_asr=lazy_asr,
        runtime=runtime,
        language=language,
        task=task,
        max_new_tokens=max_new_tokens,
        max_verification_segments=max_verification_segments,
    )


# Compatibility aliases are direct references, not decorated wrappers.  All
# callers therefore share one lazy model and each decode is timed exactly once.
WhisperRuntime = Breeze25Runtime
LazyWhisperASR = LazyBreeze25ASR
load_pinned_whisper_runtime = load_pinned_breeze25_runtime
load_pinned_verification_whisper_runtime = load_pinned_breeze25_runtime
_DEFAULT_WHISPER = _DEFAULT_BREEZE25
_DEFAULT_VERIFICATION_WHISPER = _DEFAULT_BREEZE25
_qualified_whisper_pause_ranges = _qualified_breeze25_pause_ranges
_split_short_whisper_audio_at_sustained_pause = (
    _split_short_breeze25_audio_at_sustained_pause
)
_split_whisper_audio = _split_breeze25_audio
_transcribe_whisper = _transcribe_breeze25
transcribe_whisper = transcribe_breeze25
transcribe_verification_whisper = transcribe_breeze25


@dataclass(frozen=True)
class PreparedCandidateAudio:
    """Validated candidate waveform and its ASR transcript."""

    waveform: np.ndarray
    duration_seconds: float
    transcript_text: str


def prepare_candidate_audio(
    audio: np.ndarray | Sequence[float],
    sample_rate: int,
    *,
    transcriber: Callable[[np.ndarray, int], str] | None = None,
) -> PreparedCandidateAudio | None:
    """Prepare one candidate, returning ``None`` for candidate-data errors.

    Invalid/empty/non-finite audio and a transcriber's ``ValueError`` describe
    an unusable candidate, not a service outage.  They therefore become a
    normal gate rejection so the cascade can try the next seed.  Runtime and
    I/O failures deliberately propagate and abort the request fail-closed.
    """

    try:
        selected_sample_rate = int(sample_rate)
        if selected_sample_rate <= 0:
            raise ValueError("sample_rate must be positive")
        waveform = _mono_audio(audio)
        transcript = (transcriber or transcribe_breeze25)(
            waveform,
            selected_sample_rate,
        )
        if not isinstance(transcript, str):
            raise ValueError("ASR transcript must be a string")
    except (TypeError, ValueError, OverflowError):
        return None
    return PreparedCandidateAudio(
        waveform=waveform,
        duration_seconds=waveform.size / float(selected_sample_rate),
        transcript_text=transcript.strip(),
    )


@torch.inference_mode()
def _encode_speaker_segments(
    segments: Sequence[np.ndarray],
    encoder: Any,
    *,
    device: str | torch.device,
) -> np.ndarray:
    """Encode a variable-length segment batch in one ECAPA forward pass."""

    waveforms = tuple(np.asarray(segment, dtype=np.float32).reshape(-1) for segment in segments)
    if not waveforms or any(
        waveform.size == 0 or not np.isfinite(waveform).all()
        for waveform in waveforms
    ):
        raise ValueError("speaker segments must be non-empty and finite")
    maximum_length = max(waveform.size for waveform in waveforms)
    batch = np.zeros((len(waveforms), maximum_length), dtype=np.float32)
    relative_lengths = np.empty(len(waveforms), dtype=np.float32)
    for index, waveform in enumerate(waveforms):
        batch[index, : waveform.size] = waveform
        relative_lengths[index] = waveform.size / float(maximum_length)

    selected_device = torch.device(device)
    tensor = torch.from_numpy(batch).to(selected_device)
    wav_lens = torch.from_numpy(relative_lengths).to(selected_device)
    embeddings = encoder.encode_batch(tensor, wav_lens=wav_lens)
    embeddings = torch.as_tensor(embeddings).detach().float()
    if embeddings.ndim == 0 or embeddings.shape[0] != len(waveforms):
        raise ValueError("speaker encoder returned an invalid batch size")
    embeddings = embeddings.reshape(len(waveforms), -1)
    if embeddings.shape[1] == 0 or not torch.isfinite(embeddings).all():
        raise ValueError("speaker encoder returned an invalid embedding")
    norms = torch.linalg.vector_norm(embeddings, dim=1)
    if not torch.isfinite(norms).all() or bool(torch.any(norms <= 1.0e-8)):
        raise ValueError("speaker encoder returned a zero-norm embedding")
    normalized = torch_functional.normalize(embeddings, dim=1).cpu().numpy().astype(np.float32)
    if not np.isfinite(normalized).all():
        raise ValueError("speaker encoder returned a non-finite embedding")
    return normalized


@torch.inference_mode()
def speaker_embedding_from_audio(
    audio: np.ndarray | Sequence[float],
    sample_rate: int,
    encoder: Any,
    *,
    device: str | torch.device = "cpu",
    target_sample_rate: int = 16_000,
    active_top_db: float = 35.0,
) -> np.ndarray:
    """Extract one normalized ECAPA embedding from active ndarray speech."""

    waveform = _mono_audio(audio)
    waveform = _resample_audio(waveform, int(sample_rate), int(target_sample_rate))
    waveform = _trim_active_speech(waveform, top_db=active_top_db)
    return _encode_speaker_segments((waveform,), encoder, device=device)[0]


def cosine_similarity(left: np.ndarray | Sequence[float], right: np.ndarray | Sequence[float]) -> float:
    """Return a finite cosine similarity, raising on unusable embeddings."""

    left_array = np.asarray(left, dtype=np.float64).reshape(-1)
    right_array = np.asarray(right, dtype=np.float64).reshape(-1)
    if left_array.size == 0 or left_array.shape != right_array.shape:
        raise ValueError("speaker embeddings must have equal non-empty shapes")
    if not np.isfinite(left_array).all() or not np.isfinite(right_array).all():
        raise ValueError("speaker embeddings must be finite")
    denominator = float(np.linalg.norm(left_array) * np.linalg.norm(right_array))
    if not math.isfinite(denominator) or denominator <= 1.0e-12:
        raise ValueError("speaker embeddings must have non-zero norm")
    similarity = float(np.dot(left_array, right_array) / denominator)
    if not math.isfinite(similarity):
        raise ValueError("speaker cosine similarity is non-finite")
    return float(np.clip(similarity, -1.0, 1.0))


@dataclass(frozen=True)
class SpeakerEvidence:
    similarity: float
    begin_similarity: float
    end_similarity: float
    boundary_drop: float
    active_duration_seconds: float
    speaker_embedding: np.ndarray
    active_rms_db: float


@timed_latency_stage("ecapa")
def speaker_evidence_from_audio(
    audio: np.ndarray | Sequence[float],
    sample_rate: int,
    encoder: Any,
    anchor_embedding: np.ndarray | Sequence[float],
    *,
    device: str | torch.device = "cpu",
    edge_seconds: float = 1.5,
    whole_window_seconds: float = 3.0,
    whole_max_windows: int = 4,
    active_top_db: float = 35.0,
) -> SpeakerEvidence:
    """Measure whole/begin/end anchor similarity on trimmed active speech."""

    edge_duration = _finite_float(edge_seconds, minimum=0.01)
    window_duration = _finite_float(whole_window_seconds, minimum=0.01)
    try:
        max_windows = int(whole_max_windows)
    except (TypeError, ValueError, OverflowError):
        max_windows = 0
    if edge_duration is None or window_duration is None or max_windows <= 0:
        raise ValueError("speaker window settings must be finite and positive")
    waveform = _resample_audio(_mono_audio(audio), int(sample_rate), 16_000)
    active_duration_seconds = active_voiced_duration_seconds(
        waveform,
        16_000,
        top_db=active_top_db,
    )
    active = _trim_active_speech(waveform, top_db=active_top_db)
    edge_samples = max(1, int(round(edge_duration * 16_000)))
    whole_window_samples = max(1, int(round(window_duration * 16_000)))
    begin = active[:edge_samples]
    end = active[-edge_samples:]
    if active.size <= whole_window_samples:
        whole_segments = [active]
    else:
        starts = np.linspace(
            0,
            active.size - whole_window_samples,
            num=max_windows,
        ).round().astype(int)
        whole_segments = [
            active[start : start + whole_window_samples]
            for start in dict.fromkeys(starts.tolist())
        ]
    embeddings = _encode_speaker_segments(
        (*whole_segments, begin, end),
        encoder,
        device=device,
    )
    whole_embeddings = embeddings[: len(whole_segments)]
    whole_embedding = np.mean(whole_embeddings, axis=0, dtype=np.float64)
    whole_norm = float(np.linalg.norm(whole_embedding))
    if not math.isfinite(whole_norm) or whole_norm <= 1.0e-8:
        raise ValueError("speaker windows produced a zero-norm embedding")
    whole_embedding = np.asarray(whole_embedding / whole_norm, dtype=np.float32)
    begin_embedding, end_embedding = embeddings[-2:]
    similarity = cosine_similarity(whole_embedding, anchor_embedding)
    begin_similarity = cosine_similarity(begin_embedding, anchor_embedding)
    end_similarity = cosine_similarity(end_embedding, anchor_embedding)
    boundary_drop = max(0.0, begin_similarity - end_similarity)
    active_rms = float(np.sqrt(np.mean(np.square(active, dtype=np.float64))))
    if not math.isfinite(active_rms) or active_rms <= 1.0e-8:
        raise ValueError("active speech has invalid RMS")
    return SpeakerEvidence(
        similarity=similarity,
        begin_similarity=begin_similarity,
        end_similarity=end_similarity,
        boundary_drop=boundary_drop,
        active_duration_seconds=active_duration_seconds,
        speaker_embedding=whole_embedding.copy(),
        active_rms_db=20.0 * math.log10(active_rms),
    )


@timed_latency_stage("ecapa")
def release_speaker_evidence_from_audio(
    audio: np.ndarray | Sequence[float],
    sample_rate: int,
    encoder: Any,
    anchor_embedding: np.ndarray | Sequence[float],
    *,
    device: str | torch.device = "cpu",
    active_top_db: float = 35.0,
) -> SpeakerEvidence:
    """Measure the exact full/third speaker evidence used for promotion.

    Candidate-local scoring keeps the bounded window metric for latency.  A
    returned waveform is additionally checked with this independent-aligned
    metric so a low online boundary drop cannot hide a release-gate failure.
    """

    try:
        rate = operator.index(sample_rate)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("sample rate must be positive") from error
    if rate <= 0:
        raise ValueError("sample rate must be positive")
    waveform = _mono_audio(audio)
    active_duration_seconds = active_voiced_duration_seconds(
        waveform,
        rate,
        top_db=active_top_db,
    )
    active = trim_release_speaker_activity(
        waveform,
        rate,
        top_db=active_top_db,
    )
    source_segments = (active, *tuple(np.array_split(active, 3)))
    if any(segment.size == 0 for segment in source_segments):
        raise ValueError("release speaker segments must be non-empty")

    import librosa

    segments_16k: list[np.ndarray] = []
    for segment in source_segments:
        resampled = (
            segment
            if rate == 16_000
            else librosa.resample(segment, orig_sr=rate, target_sr=16_000)
        )
        segments_16k.append(np.ascontiguousarray(resampled, dtype=np.float32))

    # Encode one segment at a time to match the independent verifier rather
    # than allowing padding/batching to perturb short boundary embeddings.
    selected_device = torch.device(device)
    encoded: list[np.ndarray] = []
    for segment in segments_16k:
        tensor = torch.from_numpy(segment).unsqueeze(0).to(selected_device)
        embedding = (
            torch.as_tensor(encoder.encode_batch(tensor))
            .detach()
            .float()
            .reshape(-1)
        )
        if embedding.numel() == 0 or not bool(torch.isfinite(embedding).all()):
            raise ValueError("speaker encoder returned an invalid embedding")
        norm = torch.linalg.vector_norm(embedding)
        if not bool(torch.isfinite(norm)) or float(norm) <= 1.0e-8:
            raise ValueError("speaker encoder returned a zero-norm embedding")
        encoded.append((embedding / norm).cpu().numpy().astype(np.float32))
    embeddings = np.stack(encoded, axis=0)
    whole_embedding = embeddings[0]
    begin_embedding = embeddings[1]
    end_embedding = embeddings[-1]
    similarity = cosine_similarity(whole_embedding, anchor_embedding)
    begin_similarity = cosine_similarity(begin_embedding, anchor_embedding)
    end_similarity = cosine_similarity(end_embedding, anchor_embedding)
    active_rms = float(np.sqrt(np.mean(np.square(active, dtype=np.float64))))
    if not math.isfinite(active_rms) or active_rms <= 1.0e-8:
        raise ValueError("active speech has invalid RMS")
    return SpeakerEvidence(
        similarity=similarity,
        begin_similarity=begin_similarity,
        end_similarity=end_similarity,
        boundary_drop=max(0.0, begin_similarity - end_similarity),
        active_duration_seconds=active_duration_seconds,
        speaker_embedding=whole_embedding.copy(),
        active_rms_db=20.0 * math.log10(active_rms),
    )


def active_audio_rms_db(audio: np.ndarray | Sequence[float], *, top_db: float = 35.0) -> float:
    """Measure finite RMS dB on the active region of candidate audio."""

    active = _trim_active_speech(_mono_audio(audio), top_db=top_db)
    rms = float(np.sqrt(np.mean(np.square(active, dtype=np.float64))))
    if not math.isfinite(rms) or rms <= 1.0e-8:
        raise ValueError("active speech has invalid RMS")
    return 20.0 * math.log10(rms)


def endpoint_tail_energy_ratio(
    audio: np.ndarray | Sequence[float],
    sample_rate: int,
    *,
    window_ms: float = ENDPOINT_TAIL_WINDOW_MS,
) -> float:
    """Measure pre-fade endpoint RMS relative to the waveform peak.

    The ratio is intentionally scale-independent and is measured before final
    fade/padding. A strongly voiced forced endpoint approaches one, while a
    naturally quiet endpoint approaches zero.
    """

    waveform = _mono_audio(audio)
    if isinstance(sample_rate, (bool, np.bool_)):
        raise ValueError("sample_rate must be a positive integer")
    try:
        rate = operator.index(sample_rate)
        duration_ms = float(window_ms)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("endpoint window must be finite and positive") from error
    if rate <= 0 or not math.isfinite(duration_ms) or duration_ms <= 0.0:
        raise ValueError("endpoint window must be finite and positive")
    window = max(1, int(round(duration_ms * int(rate) / 1000.0)))
    tail = waveform[-min(waveform.size, window) :]
    peak = float(np.max(np.abs(waveform)))
    if not math.isfinite(peak) or peak <= 1.0e-8:
        return 0.0
    tail_rms = float(np.sqrt(np.mean(np.square(tail, dtype=np.float64))))
    if not math.isfinite(tail_rms) or tail_rms < 0.0:
        raise ValueError("endpoint energy is invalid")
    return float(np.clip(tail_rms / peak, 0.0, 1.0))


@timed_latency_stage("f0")
def active_audio_median_f0_hz(
    audio: np.ndarray | Sequence[float],
    sample_rate: int,
    *,
    fmin_hz: float = 65.0,
    fmax_hz: float = 500.0,
) -> float | None:
    """Return a robust voiced-pitch median for soft sequence transitions."""

    waveform = _mono_audio(audio)
    if sample_rate <= 0 or waveform.size < 1024:
        return None
    try:
        import librosa

        f0, voiced, _ = librosa.pyin(
            waveform,
            fmin=float(fmin_hz),
            fmax=float(fmax_hz),
            sr=int(sample_rate),
            frame_length=1024,
            hop_length=256,
        )
    except (ImportError, FloatingPointError, TypeError, ValueError):
        return None
    valid = np.asarray(f0, dtype=np.float64)
    if voiced is not None:
        valid = valid[np.asarray(voiced, dtype=bool)]
    valid = valid[np.isfinite(valid)]
    if valid.size == 0:
        return None
    median = float(np.median(valid))
    return median if math.isfinite(median) and median > 0.0 else None


@dataclass(frozen=True)
class CandidateObservation:
    target_text: str
    transcript_text: str
    audio_duration_seconds: float
    speaker_similarity: float | None = None
    begin_speaker_similarity: float | None = None
    end_speaker_similarity: float | None = None
    pace_cps: float | None = None
    squim_stoi: float | None = None
    squim_pesq: float | None = None
    squim_si_sdr: float | None = None
    truncated: bool = False


@dataclass(frozen=True)
class CandidateGateResult:
    passed: bool
    comparison: AsrComparison
    audio_duration_seconds: float | None
    speaker_gate_applied: bool
    speaker_similarity: float | None
    boundary_speaker_drop: float | None
    pace_cps: float | None
    squim_gate_applied: bool
    squim_stoi: float | None
    squim_pesq: float | None
    squim_si_sdr: float | None
    squim_quality_cost: float | None
    score: float
    rejection_reasons: tuple[str, ...]


@dataclass(frozen=True)
class ChunkCandidateArtifact:
    """Acoustic evidence retained for sequence-level candidate selection."""

    speaker_embedding: np.ndarray | None = None
    rms_db: float | None = None
    median_f0_hz: float | None = None
    stop_reason: str | None = None
    endpoint_energy_ratio: float | None = None
    generated_steps: int | None = None
    hard_stop_steps: int | None = None


@dataclass(frozen=True)
class CandidateGateEvidence:
    """Bounded, content-free diagnostic snapshot of one hard-gate result."""

    passed: bool
    target_units: int
    hypothesis_units: int
    edit_distance: int
    cer: float | None
    prefix_cer: float | None
    suffix_cer: float | None
    prefix_deletions: int
    suffix_deletions: int
    extra_tail_units: int
    audio_duration_seconds: float | None
    speaker_gate_applied: bool
    speaker_similarity: float | None
    boundary_speaker_drop: float | None
    pace_cps: float | None
    squim_gate_applied: bool
    squim_stoi: float | None
    squim_pesq: float | None
    squim_si_sdr: float | None
    squim_quality_cost: float | None
    score: float | None
    rejection_reasons: tuple[str, ...]


@dataclass(frozen=True)
class TrajectoryGateEvidence:
    """Content-free evidence for a joined, sequence, or final waveform gate."""

    passed: bool
    result_count: int
    score: float | None
    rejection_reasons: tuple[str, ...]
    result: CandidateGateEvidence | None


@dataclass(frozen=True)
class LocalIndependentGateEvidence:
    """Bounded per-row attestation for independent local semantic ASR."""

    attempted: bool
    passed: bool | None
    proof_count: int
    result: CandidateGateEvidence | None


@dataclass(frozen=True)
class CandidateGenerationEvidence:
    """Content-free CFG and source-row evidence for one generation call."""

    chunk_indices: tuple[int, ...]
    chunk_text_units: tuple[int, ...]
    scheduled_cfg: float
    effective_cfgs: tuple[float, ...]
    floor_reasons: tuple[tuple[str, ...], ...]
    chunk_candidate_ordinals: tuple[int, ...] = ()
    network_conditioned: tuple[bool, ...] = ()
    chunk_text_variants: tuple[str, ...] = ()
    chunk_stop_reasons: tuple[str, ...] = ()
    chunk_endpoint_energy_ratios: tuple[float, ...] = ()
    chunk_generated_steps: tuple[int, ...] = ()
    chunk_hard_stop_steps: tuple[int, ...] = ()


@dataclass(frozen=True)
class CandidateAttemptEvidence:
    """Diagnostics for one generated trajectory without waveform/text payloads."""

    candidate_index: int
    seed: int
    trajectory_passed: bool
    trajectory_score: float | None
    trajectory_rejection_reasons: tuple[str, ...]
    local_result_count: int
    local_results: tuple[CandidateGateEvidence, ...]
    chunk_indices: tuple[int, ...]
    chunk_text_units: tuple[int, ...]
    chunk_candidate_ordinals: tuple[int, ...]
    network_conditioned: tuple[bool, ...]
    chunk_text_variants: tuple[str, ...]
    chunk_stop_reasons: tuple[str, ...]
    chunk_endpoint_energy_ratios: tuple[float, ...]
    chunk_generated_steps: tuple[int, ...]
    chunk_hard_stop_steps: tuple[int, ...]
    scheduled_cfg: float | None
    effective_cfgs: tuple[float, ...]
    floor_reasons: tuple[tuple[str, ...], ...]
    independent_local_results: tuple[LocalIndependentGateEvidence, ...] = ()
    joined_output: TrajectoryGateEvidence | None = None
    independent_output: TrajectoryGateEvidence | None = None


@dataclass(frozen=True)
class SequencePathEvidence:
    """Final-verifier evidence in bounded check order across expanding lattices."""

    rank: int
    chunk_candidate_indices: tuple[int, ...]
    chunk_seeds: tuple[int, ...]
    final_output: TrajectoryGateEvidence


@dataclass(frozen=True)
class SequenceSearchEvidence:
    """Finite-lattice diagnostics and bounded exact-verifier search evidence.

    ``ranked_path_count`` counts verifier-order paths accumulated across the
    incremental and completed-lattice searches; it is not a global rank count
    recomputed against only the final lattice.
    """

    eligible_candidate_counts: tuple[int, ...]
    finite_transition_counts: tuple[int, ...]
    ranked_path_count: int
    checked_paths: tuple[SequencePathEvidence, ...] = ()


@dataclass(frozen=True)
class CascadeDiagnostics:
    """Bounded diagnostics retained on both success and fail-closed outcomes."""

    attempts: tuple[CandidateAttemptEvidence, ...] = ()
    sequence_search: SequenceSearchEvidence | None = None


def _evidence_float(value: Any) -> float | None:
    return _finite_float(value)


def _evidence_int(value: Any, *, maximum: int = 1_000_000) -> int:
    if isinstance(value, (bool, np.bool_)):
        return 0
    try:
        integer = operator.index(value)
    except (TypeError, ValueError, OverflowError):
        return 0
    return min(max(0, int(integer)), maximum)


def _sanitize_rejection_reason(reason: Any) -> str:
    """Return an allow-listed reason code without forwarding arbitrary text."""

    if not isinstance(reason, str) or len(reason) > 96:
        return "unknown_rejection"
    tokens = reason.split(":")
    if (
        not tokens
        or len(tokens) > 3
        or tokens[-1] not in _CASCADE_EVIDENCE_REJECTION_CODES
    ):
        return "unknown_rejection"
    for token in tokens[:-1]:
        if token == "joined_output":
            continue
        if token.startswith("chunk_"):
            chunk_index = token[6:]
            if (
                chunk_index.isdigit()
                and len(chunk_index) <= 2
                and int(chunk_index) < CASCADE_EVIDENCE_MAX_LOCAL_RESULTS
            ):
                continue
        return "unknown_rejection"
    return ":".join(tokens)


def _bounded_rejection_reasons(reasons: Any) -> tuple[str, ...]:
    try:
        values = tuple(reasons)
    except TypeError:
        values = ()
    return tuple(
        _sanitize_rejection_reason(reason)
        for reason in values[:CASCADE_EVIDENCE_MAX_REASONS]
    )


def candidate_gate_evidence(result: CandidateGateResult) -> CandidateGateEvidence:
    """Project a gate result to finite scalar/count evidence only."""

    if not isinstance(result, CandidateGateResult):
        raise TypeError("result must be a CandidateGateResult")
    comparison = result.comparison
    return CandidateGateEvidence(
        passed=result.passed is True,
        target_units=_evidence_int(len(comparison.target_text)),
        hypothesis_units=_evidence_int(len(comparison.transcript_text)),
        edit_distance=_evidence_int(comparison.edit_distance),
        cer=_evidence_float(comparison.cer),
        prefix_cer=_evidence_float(comparison.prefix_cer),
        suffix_cer=_evidence_float(comparison.suffix_cer),
        prefix_deletions=_evidence_int(comparison.prefix_deletions),
        suffix_deletions=_evidence_int(comparison.suffix_deletions),
        extra_tail_units=_evidence_int(comparison.extra_tail_units),
        audio_duration_seconds=_evidence_float(result.audio_duration_seconds),
        speaker_gate_applied=result.speaker_gate_applied is True,
        speaker_similarity=_evidence_float(result.speaker_similarity),
        boundary_speaker_drop=_evidence_float(result.boundary_speaker_drop),
        pace_cps=_evidence_float(result.pace_cps),
        squim_gate_applied=result.squim_gate_applied is True,
        squim_stoi=_evidence_float(result.squim_stoi),
        squim_pesq=_evidence_float(result.squim_pesq),
        squim_si_sdr=_evidence_float(result.squim_si_sdr),
        squim_quality_cost=_evidence_float(result.squim_quality_cost),
        score=_evidence_float(result.score),
        rejection_reasons=_bounded_rejection_reasons(result.rejection_reasons),
    )


def verify_candidate(
    observation: CandidateObservation,
    *,
    locale: str = "zh-TW",
    short_text_units: int = 6,
    short_text_max_cer: float = 0.0,
    max_cer: float = 0.20,
    prefix_units: int = 6,
    suffix_units: int = 6,
    max_prefix_cer: float = 0.0,
    max_suffix_cer: float = 0.0,
    max_prefix_deletions: int | None = None,
    max_suffix_deletions: int | None = None,
    max_extra_tail_units: int = 0,
    speaker_gate_enabled: bool = True,
    short_audio_seconds: float = 1.5,
    min_speaker_similarity: float = 0.10,
    max_boundary_speaker_drop: float = 0.03,
    max_pace_cps: float | None = None,
    squim_gate_enabled: bool = False,
    min_squim_stoi: float = 0.60,
    min_squim_pesq: float = 1.12,
    squim_stoi_weight: float = 0.03,
    squim_pesq_weight: float = 0.02,
    squim_si_sdr_weight: float = 0.01,
    speaker_weight: float = 0.05,
    boundary_weight: float = 0.10,
    skip_acoustic_on_hard_failure: bool = False,
) -> CandidateGateResult:
    """Apply strict semantic and duration-aware speaker gates to a candidate."""

    duration = _finite_float(observation.audio_duration_seconds, minimum=0.0)
    short_duration_limit = _finite_float(short_audio_seconds, minimum=0.0)
    general_cer_limit = _finite_float(max_cer, minimum=0.0)
    exact_cer_limit = _finite_float(short_text_max_cer, minimum=0.0)
    min_similarity = _finite_float(min_speaker_similarity, minimum=-1.0, maximum=1.0)
    max_boundary = _finite_float(max_boundary_speaker_drop, minimum=0.0)
    max_pace = None if max_pace_cps is None else _finite_float(max_pace_cps, minimum=0.0)
    min_stoi = _finite_float(min_squim_stoi, minimum=0.0, maximum=1.0)
    min_pesq = _finite_float(min_squim_pesq, minimum=0.0, maximum=5.0)
    stoi_cost_weight = _finite_float(squim_stoi_weight, minimum=0.0)
    pesq_cost_weight = _finite_float(squim_pesq_weight, minimum=0.0)
    si_sdr_cost_weight = _finite_float(squim_si_sdr_weight, minimum=0.0)
    speaker_cost_weight = _finite_float(speaker_weight, minimum=0.0)
    boundary_cost_weight = _finite_float(boundary_weight, minimum=0.0)
    try:
        short_unit_limit = max(0, int(short_text_units))
    except (TypeError, ValueError, OverflowError):
        short_unit_limit = -1
    deletion_limits: list[int | None] = []
    deletion_config_valid = True
    for value in (max_prefix_deletions, max_suffix_deletions):
        if value is None:
            deletion_limits.append(None)
            continue
        if isinstance(value, (bool, np.bool_)):
            deletion_limits.append(None)
            deletion_config_valid = False
            continue
        try:
            limit = operator.index(value)
        except (TypeError, ValueError, OverflowError):
            deletion_limits.append(None)
            deletion_config_valid = False
            continue
        deletion_limits.append(int(limit))
        if limit < 0:
            deletion_config_valid = False
    speaker_enabled = isinstance(speaker_gate_enabled, (bool, np.bool_))
    if speaker_enabled:
        speaker_enabled = bool(speaker_gate_enabled)
    squim_enabled = isinstance(squim_gate_enabled, (bool, np.bool_))
    if squim_enabled:
        squim_enabled = bool(squim_gate_enabled)
    skip_acoustic_enabled = isinstance(
        skip_acoustic_on_hard_failure,
        (bool, np.bool_),
    )
    if skip_acoustic_enabled:
        skip_acoustic_enabled = bool(skip_acoustic_on_hard_failure)

    # First normalize with a permissive finite limit to determine target units.
    preliminary = compare_asr_text(
        observation.target_text,
        observation.transcript_text,
        locale=locale,
        prefix_units=prefix_units,
        suffix_units=suffix_units,
        max_cer=general_cer_limit if general_cer_limit is not None else math.nan,
        max_prefix_cer=max_prefix_cer,
        max_suffix_cer=max_suffix_cer,
        max_extra_tail_units=max_extra_tail_units,
    )
    selected_cer_limit = general_cer_limit
    if short_unit_limit >= 0 and len(preliminary.target_text) <= short_unit_limit:
        selected_cer_limit = exact_cer_limit
    comparison = compare_asr_text(
        observation.target_text,
        observation.transcript_text,
        locale=locale,
        prefix_units=prefix_units,
        suffix_units=suffix_units,
        max_cer=selected_cer_limit if selected_cer_limit is not None else math.nan,
        max_prefix_cer=max_prefix_cer,
        max_suffix_cer=max_suffix_cer,
        max_extra_tail_units=max_extra_tail_units,
    )

    reasons: list[str] = []
    if duration is None or duration <= 0.0:
        reasons.append("invalid_audio_duration")
    if observation.truncated is not False:
        reasons.append("truncated")
    deletion_gate_passed = bool(
        deletion_config_valid
        and (
            deletion_limits[0] is None
            or comparison.prefix_deletions <= deletion_limits[0]
        )
        and (
            deletion_limits[1] is None
            or comparison.suffix_deletions <= deletion_limits[1]
        )
    )
    if not comparison.passed or (
        deletion_config_valid and not deletion_gate_passed
    ):
        reasons.append("semantic_gate")
    if (
        comparison.network_protected_spans > 0
        and comparison.network_protected_spans_passed is not True
    ):
        reasons.append("network_protected_span_mismatch")
    pace = None
    if max_pace_cps is not None:
        pace = _finite_float(observation.pace_cps, minimum=0.0)
        if max_pace is None:
            reasons.append("invalid_gate_config")
        elif pace is None:
            reasons.append("missing_pace_evidence")
        elif pace > max_pace:
            reasons.append("pace_too_fast")

    hard_failure_before_acoustics = bool(reasons)
    skip_acoustic = bool(
        skip_acoustic_enabled and hard_failure_before_acoustics
    )
    squim_gate_applied = bool(squim_enabled and not skip_acoustic)
    squim_stoi: float | None = None
    squim_pesq: float | None = None
    squim_si_sdr: float | None = None
    squim_quality_cost: float | None = None
    if squim_gate_applied:
        squim_stoi = _finite_float(
            observation.squim_stoi,
            minimum=0.0,
            maximum=1.0,
        )
        squim_pesq = _finite_float(
            observation.squim_pesq,
            minimum=0.0,
            maximum=5.0,
        )
        squim_si_sdr = _finite_float(observation.squim_si_sdr)
        if (
            squim_stoi is None
            or squim_pesq is None
            or squim_si_sdr is None
        ):
            reasons.append("missing_squim_evidence")
        else:
            if min_stoi is None or squim_stoi < min_stoi:
                reasons.append("squim_stoi_too_low")
            if min_pesq is None or squim_pesq < min_pesq:
                reasons.append("squim_pesq_too_low")
            if all(
                weight is not None
                for weight in (
                    stoi_cost_weight,
                    pesq_cost_weight,
                    si_sdr_cost_weight,
                )
            ):
                assert squim_stoi is not None
                assert squim_pesq is not None
                assert squim_si_sdr is not None
                squim_quality_cost = (
                    stoi_cost_weight * float(np.clip(1.0 - squim_stoi, 0.0, 1.0))
                    + pesq_cost_weight
                    * float(np.clip((4.5 - squim_pesq) / 3.5, 0.0, 1.0))
                    + si_sdr_cost_weight
                    * float(np.clip((20.0 - squim_si_sdr) / 40.0, 0.0, 1.0))
                )

    valid_common_config = bool(
        isinstance(speaker_gate_enabled, (bool, np.bool_))
        and short_duration_limit is not None
        and general_cer_limit is not None
        and exact_cer_limit is not None
        and short_unit_limit >= 0
        and deletion_config_valid
        and isinstance(squim_gate_enabled, (bool, np.bool_))
        and isinstance(skip_acoustic_on_hard_failure, (bool, np.bool_))
        and (
            not squim_enabled
            or all(
                value is not None
                for value in (
                    min_stoi,
                    min_pesq,
                    stoi_cost_weight,
                    pesq_cost_weight,
                    si_sdr_cost_weight,
                )
            )
        )
        and (
            not speaker_enabled
            or all(
                value is not None
                for value in (
                    min_similarity,
                    max_boundary,
                    speaker_cost_weight,
                    boundary_cost_weight,
                )
            )
        )
    )
    if not valid_common_config:
        reasons.append("invalid_gate_config")

    speaker_gate_applied = bool(
        speaker_enabled
        and not skip_acoustic
        and duration is not None
        and short_duration_limit is not None
        and duration >= short_duration_limit
    )
    similarity: float | None = None
    boundary_drop: float | None = None
    if speaker_gate_applied:
        similarity = _finite_float(
            observation.speaker_similarity,
            minimum=-1.0,
            maximum=1.0,
        )
        begin_similarity = _finite_float(
            observation.begin_speaker_similarity,
            minimum=-1.0,
            maximum=1.0,
        )
        end_similarity = _finite_float(
            observation.end_speaker_similarity,
            minimum=-1.0,
            maximum=1.0,
        )
        if similarity is None or begin_similarity is None or end_similarity is None:
            reasons.append("missing_speaker_evidence")
        else:
            boundary_drop = max(0.0, begin_similarity - end_similarity)
            if min_similarity is None or similarity < min_similarity:
                reasons.append("speaker_similarity")
            if max_boundary is None or boundary_drop > max_boundary:
                reasons.append("boundary_speaker_drop")

    score = math.inf
    if not reasons:
        score = comparison.cer
        if speaker_gate_applied:
            assert similarity is not None and boundary_drop is not None
            assert speaker_cost_weight is not None and boundary_cost_weight is not None
            score += speaker_cost_weight * (1.0 - similarity)
            score += boundary_cost_weight * boundary_drop
        if squim_gate_applied:
            assert squim_quality_cost is not None
            score += squim_quality_cost
        if not math.isfinite(score) or score < 0.0:
            reasons.append("nonfinite_score")
            score = math.inf

    return CandidateGateResult(
        passed=not reasons,
        comparison=comparison,
        audio_duration_seconds=duration,
        speaker_gate_applied=speaker_gate_applied,
        speaker_similarity=similarity,
        boundary_speaker_drop=boundary_drop,
        pace_cps=pace,
        squim_gate_applied=squim_gate_applied,
        squim_stoi=squim_stoi,
        squim_pesq=squim_pesq,
        squim_si_sdr=squim_si_sdr,
        squim_quality_cost=squim_quality_cost,
        score=score,
        rejection_reasons=tuple(reasons),
    )


@dataclass(frozen=True)
class TrajectoryGateResult:
    passed: bool
    candidate_results: tuple[CandidateGateResult, ...]
    score: float
    rejection_reasons: tuple[str, ...]
    chunk_artifacts: tuple[ChunkCandidateArtifact, ...] = ()


@dataclass(frozen=True)
class CandidateVerification:
    """Selection result plus content-free whole-output gate evidence."""

    verification: TrajectoryGateResult
    independent_local_results: tuple[LocalIndependentGateEvidence, ...] = ()
    joined_output: TrajectoryGateEvidence | None = None
    independent_output: TrajectoryGateEvidence | None = None


def trajectory_gate_evidence(
    verification: TrajectoryGateResult,
) -> TrajectoryGateEvidence:
    """Project a joined/final verification without retaining recognized text."""

    if not isinstance(verification, TrajectoryGateResult):
        raise TypeError("verification must be a TrajectoryGateResult")
    sole_result = (
        verification.candidate_results[0]
        if len(verification.candidate_results) == 1
        else None
    )
    result = (
        candidate_gate_evidence(sole_result)
        if isinstance(sole_result, CandidateGateResult)
        else None
    )
    return TrajectoryGateEvidence(
        passed=verification.passed is True,
        result_count=_evidence_int(len(verification.candidate_results)),
        score=_evidence_float(verification.score),
        rejection_reasons=_bounded_rejection_reasons(
            verification.rejection_reasons
        ),
        result=result,
    )


def exact_waveform_sha256(
    audio: np.ndarray | Sequence[float],
    sample_rate: int,
) -> str:
    """Hash exact canonical float32 samples together with their sample rate."""

    if isinstance(sample_rate, (bool, np.bool_)):
        raise ValueError("sample_rate must be a positive integer")
    try:
        rate = operator.index(sample_rate)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("sample_rate must be a positive integer") from error
    if rate <= 0:
        raise ValueError("sample_rate must be a positive integer")
    waveform = _mono_audio(audio)
    canonical = np.ascontiguousarray(waveform, dtype=np.dtype("<f4"))
    digest = hashlib.sha256()
    digest.update(b"bluemagpie-whole-waveform-f32le-v1\0")
    digest.update(int(rate).to_bytes(8, "little", signed=False))
    digest.update(int(canonical.size).to_bytes(8, "little", signed=False))
    digest.update(memoryview(canonical).cast("B"))
    return digest.hexdigest()


class WholeWaveformVerificationCache:
    """Request-local cache keyed by exact audio, target and verifier profile.

    The cache deliberately accepts no process-global state.  A caller must
    instantiate it inside one synthesis request, and verifier calls that raise
    are never cached.  Passed and rejected gate evidence are both deterministic
    evidence and may be reused only for an exact key match.
    """

    def __init__(self) -> None:
        self._entries: dict[
            tuple[str, int, str, str],
            TrajectoryGateResult,
        ] = {}

    @property
    def entry_count(self) -> int:
        return len(self._entries)

    def verify(
        self,
        audio: np.ndarray | Sequence[float],
        sample_rate: int,
        target_text: str,
        verifier_profile: str,
        verifier: Callable[[np.ndarray, int, str], TrajectoryGateResult],
    ) -> TrajectoryGateResult:
        if not isinstance(target_text, str) or not target_text:
            raise ValueError("target_text must be a non-empty string")
        if not isinstance(verifier_profile, str) or not verifier_profile:
            raise ValueError("verifier_profile must be a non-empty string")
        if not callable(verifier):
            raise ValueError("verifier must be callable")
        waveform = _mono_audio(audio)
        if isinstance(sample_rate, (bool, np.bool_)):
            raise ValueError("sample_rate must be a positive integer")
        try:
            rate = operator.index(sample_rate)
        except (TypeError, ValueError, OverflowError) as error:
            raise ValueError("sample_rate must be a positive integer") from error
        if rate <= 0:
            raise ValueError("sample_rate must be a positive integer")
        waveform_hash = exact_waveform_sha256(waveform, rate)
        key = (waveform_hash, rate, target_text, verifier_profile)
        cached = self._entries.get(key)
        if cached is not None:
            return cached
        verification = verifier(waveform, rate, target_text)
        if not isinstance(verification, TrajectoryGateResult):
            raise RuntimeError("whole-waveform verifier returned an invalid result")
        self._entries[key] = verification
        return verification


class WholeWaveformTranscriptCache:
    """Request-local ASR cache for one exact waveform and decoder profile.

    Targets are intentionally absent from the key: transcription depends on
    the immutable audio and pinned decoder contract, not on the text used by a
    later semantic comparison.  Exceptions and non-string results are never
    cached.
    """

    def __init__(self) -> None:
        self._entries: dict[tuple[str, int, str], str] = {}

    @property
    def entry_count(self) -> int:
        return len(self._entries)

    def transcribe(
        self,
        audio: np.ndarray | Sequence[float],
        sample_rate: int,
        decoder_profile: str,
        transcriber: Callable[[np.ndarray, int], str],
    ) -> str:
        if not isinstance(decoder_profile, str) or not decoder_profile:
            raise ValueError("decoder_profile must be a non-empty string")
        if not callable(transcriber):
            raise ValueError("transcriber must be callable")
        waveform = _mono_audio(audio)
        if isinstance(sample_rate, (bool, np.bool_)):
            raise ValueError("sample_rate must be a positive integer")
        try:
            rate = operator.index(sample_rate)
        except (TypeError, ValueError, OverflowError) as error:
            raise ValueError("sample_rate must be a positive integer") from error
        if rate <= 0:
            raise ValueError("sample_rate must be a positive integer")
        key = (
            exact_waveform_sha256(waveform, rate),
            rate,
            decoder_profile,
        )
        cached = self._entries.get(key)
        if cached is not None:
            return cached
        transcript = transcriber(waveform, rate)
        if not isinstance(transcript, str):
            raise RuntimeError("whole-waveform transcriber returned an invalid result")
        self._entries[key] = transcript
        return transcript


def verify_trajectory(
    observations: Sequence[CandidateObservation],
    *,
    chunk_artifacts: Sequence[ChunkCandidateArtifact] = (),
    candidate_gate_kwargs_by_index: Sequence[dict[str, Any]] | None = None,
    **candidate_gate_kwargs: Any,
) -> TrajectoryGateResult:
    """Require every chunk in a non-empty trajectory to pass all hard gates."""

    try:
        candidates = tuple(observations)
    except TypeError:
        candidates = ()
    if not candidates:
        return TrajectoryGateResult(False, (), math.inf, ("empty_trajectory",))

    try:
        artifacts = tuple(chunk_artifacts)
    except TypeError:
        artifacts = ()
    if artifacts and (
        len(artifacts) != len(candidates)
        or any(not isinstance(artifact, ChunkCandidateArtifact) for artifact in artifacts)
    ):
        return TrajectoryGateResult(
            False,
            (),
            math.inf,
            ("invalid_chunk_artifacts",),
        )

    if candidate_gate_kwargs_by_index is None:
        indexed_gate_kwargs = ({},) * len(candidates)
    else:
        raw_indexed_gate_kwargs: tuple[Any, ...] = ()
        try:
            raw_indexed_gate_kwargs = tuple(candidate_gate_kwargs_by_index)
            indexed_gate_kwargs = tuple(
                dict(kwargs)
                for kwargs in raw_indexed_gate_kwargs
                if type(kwargs) is dict
                and set(kwargs).issubset(_CANDIDATE_GATE_OVERRIDE_KEYS)
            )
        except (TypeError, ValueError):
            indexed_gate_kwargs = ()
        if (
            len(raw_indexed_gate_kwargs) != len(candidates)
            or len(indexed_gate_kwargs) != len(candidates)
        ):
            return TrajectoryGateResult(
                False,
                (),
                math.inf,
                ("invalid_candidate_gate_overrides",),
            )

    results: list[CandidateGateResult] = []
    rejection_reasons: list[str] = []
    for index, (observation, indexed_kwargs) in enumerate(
        zip(candidates, indexed_gate_kwargs, strict=True)
    ):
        try:
            result = verify_candidate(
                observation,
                **{**candidate_gate_kwargs, **indexed_kwargs},
            )
        except (TypeError, ValueError, OverflowError):
            # A malformed observation must reject the entire trajectory.
            comparison = compare_asr_text("", "")
            result = CandidateGateResult(
                passed=False,
                comparison=comparison,
                audio_duration_seconds=None,
                speaker_gate_applied=False,
                speaker_similarity=None,
                boundary_speaker_drop=None,
                pace_cps=None,
                squim_gate_applied=False,
                squim_stoi=None,
                squim_pesq=None,
                squim_si_sdr=None,
                squim_quality_cost=None,
                score=math.inf,
                rejection_reasons=("malformed_observation",),
            )
        results.append(result)
        rejection_reasons.extend(f"chunk_{index}:{reason}" for reason in result.rejection_reasons)
    passed = bool(results) and all(result.passed for result in results)
    score = sum(result.score for result in results) if passed else math.inf
    if not math.isfinite(score):
        passed = False
        score = math.inf
        if not rejection_reasons:
            rejection_reasons.append("nonfinite_trajectory_score")
    return TrajectoryGateResult(
        passed=passed,
        candidate_results=tuple(results),
        score=score,
        rejection_reasons=tuple(rejection_reasons),
        chunk_artifacts=artifacts,
    )


def qualify_trajectory_with_joined_output(
    local_verification: TrajectoryGateResult,
    joined_verification: TrajectoryGateResult,
) -> TrajectoryGateResult:
    """Require joined-output safety without discarding DP-local evidence.

    ``candidate_results`` and ``chunk_artifacts`` always stay local to the
    generated chunks.  This lets sequence DP reuse individually safe chunks
    when RMS matching, fades, pauses or crossfade make the same-seed joined
    waveform fail its whole-output gate.
    """

    if not isinstance(local_verification, TrajectoryGateResult):
        raise TypeError("local_verification must be a TrajectoryGateResult")
    if local_verification.passed is not True or not math.isfinite(
        local_verification.score
    ):
        return local_verification
    if not isinstance(joined_verification, TrajectoryGateResult):
        raise TypeError("joined_verification must be a TrajectoryGateResult")

    joined_passed = bool(
        joined_verification.passed is True
        and math.isfinite(joined_verification.score)
        and len(joined_verification.candidate_results) == 1
    )
    if joined_passed:
        return local_verification

    joined_reasons = joined_verification.rejection_reasons or (
        "invalid_joined_verification",
    )
    return TrajectoryGateResult(
        passed=False,
        candidate_results=local_verification.candidate_results,
        score=math.inf,
        rejection_reasons=tuple(
            f"joined_output:{reason}" for reason in joined_reasons
        ),
        chunk_artifacts=local_verification.chunk_artifacts,
    )


def intersect_local_semantic_verification(
    primary_verification: TrajectoryGateResult,
    independent_verification: TrajectoryGateResult,
    primary_indices: Sequence[int],
) -> TrajectoryGateResult:
    """Hard-intersect selected local rows without replacing acoustic evidence.

    ``primary_verification`` owns the Breeze25, speaker, SQUIM, pace and
    boundary-proxy evidence consumed by the coverage selector.
    ``independent_verification`` contains a semantic-only projection for the
    selected network-conditioned rows in the order given by
    ``primary_indices``. A secondary rejection is projected onto the
    corresponding primary row with allow-listed semantic reason codes, making
    it impossible for either the strict pool or the boundary-only proxy to
    retain that local candidate. Production reuses the exact Breeze25
    transcript cache, so this projection does not decode twice. Passing
    intersections return the original primary object unchanged.
    """

    if not isinstance(primary_verification, TrajectoryGateResult):
        raise TypeError("primary_verification must be a TrajectoryGateResult")
    if not isinstance(independent_verification, TrajectoryGateResult):
        raise TypeError(
            "independent_verification must be a TrajectoryGateResult"
        )
    try:
        selected_indices = tuple(primary_indices)
    except TypeError as error:
        raise ValueError("primary_indices must be an ordered index sequence") from error
    if (
        not selected_indices
        or any(
            isinstance(index, (bool, np.bool_))
            or not isinstance(index, int)
            for index in selected_indices
        )
        or selected_indices != tuple(sorted(set(selected_indices)))
        or any(
            index < 0
            or index >= len(primary_verification.candidate_results)
            for index in selected_indices
        )
        or len(independent_verification.candidate_results)
        != len(selected_indices)
        or any(
            not isinstance(result, CandidateGateResult)
            for result in independent_verification.candidate_results
        )
    ):
        raise ValueError(
            "independent local verification rows do not match primary indices"
        )

    failed_secondary_rows = {
        local_index
        for local_index, result in enumerate(
            independent_verification.candidate_results
        )
        if (
            result.passed is not True
            or not math.isfinite(result.score)
            or bool(result.rejection_reasons)
        )
    }
    if independent_verification.passed is not True and not failed_secondary_rows:
        # A malformed/non-finite trajectory-level result must not allow any of
        # its apparently passing local projections into the coverage pool.
        failed_secondary_rows.update(range(len(selected_indices)))
    if not failed_secondary_rows:
        return primary_verification

    combined_results = list(primary_verification.candidate_results)
    for local_index in sorted(failed_secondary_rows):
        primary_index = selected_indices[local_index]
        primary_result = combined_results[primary_index]
        independent_result = independent_verification.candidate_results[
            local_index
        ]
        semantic_reasons = ["semantic_gate"]
        if (
            "network_protected_span_mismatch"
            in independent_result.rejection_reasons
        ):
            semantic_reasons.append("network_protected_span_mismatch")
        combined_results[primary_index] = replace(
            primary_result,
            passed=False,
            score=math.inf,
            rejection_reasons=tuple(
                dict.fromkeys(
                    (*primary_result.rejection_reasons, *semantic_reasons)
                )
            ),
        )

    rejection_reasons = tuple(
        f"chunk_{index}:{reason}"
        for index, result in enumerate(combined_results)
        for reason in result.rejection_reasons
    )
    return TrajectoryGateResult(
        passed=False,
        candidate_results=tuple(combined_results),
        score=math.inf,
        rejection_reasons=rejection_reasons,
        chunk_artifacts=primary_verification.chunk_artifacts,
    )


class NoQualifiedCandidateError(RuntimeError):
    """Raised when the full adaptive cascade has no verified trajectory."""

    def __init__(
        self,
        message: str,
        *,
        diagnostics: CascadeDiagnostics | None = None,
    ) -> None:
        super().__init__(message)
        self.diagnostics = diagnostics or CascadeDiagnostics()


class FinalOutputRejectedError(RuntimeError):
    """Raised when post-join output fails the final whole-waveform gate."""


def require_verified_final_output(
    verification: TrajectoryGateResult,
) -> TrajectoryGateResult:
    """Return verified final evidence or reject without an audio fallback."""

    if not isinstance(verification, TrajectoryGateResult):
        raise FinalOutputRejectedError("final verifier returned an invalid result")
    if (
        verification.passed is not True
        or not verification.candidate_results
        or not all(result.passed for result in verification.candidate_results)
        or not math.isfinite(verification.score)
    ):
        reasons = ",".join(verification.rejection_reasons) or "unsafe_final_output"
        raise FinalOutputRejectedError(f"final output rejected: {reasons}")
    return verification


@dataclass(frozen=True)
class CascadeResult:
    """One selected trajectory and its bounded verification evidence.

    ``sequence_path_rank`` is the exact-verifier order across the progressively
    expanded request-local lattice. It is not a global rank recomputed after
    every later refill.
    """

    trajectory: Any
    verification: TrajectoryGateResult
    seed: int | None
    candidate_index: int | None
    attempted_seeds: tuple[int, ...]
    chunk_candidate_indices: tuple[int, ...] = ()
    chunk_seeds: tuple[int, ...] = ()
    selection_mode: str = "whole_trajectory"
    sequence_path_rank: int | None = None
    sequence_paths_checked: int = 0
    diagnostics: CascadeDiagnostics = CascadeDiagnostics()
    generated_chunk_count: int = 0
    generated_text_units: int = 0
    chunk_candidate_counts: tuple[int, ...] = ()


def _candidate_gate_evidence_payload(
    evidence: CandidateGateEvidence,
) -> dict[str, Any]:
    return {
        "passed": evidence.passed,
        "target_units": evidence.target_units,
        "hypothesis_units": evidence.hypothesis_units,
        "edit_distance": evidence.edit_distance,
        "cer": evidence.cer,
        "prefix_cer": evidence.prefix_cer,
        "suffix_cer": evidence.suffix_cer,
        "prefix_deletions": evidence.prefix_deletions,
        "suffix_deletions": evidence.suffix_deletions,
        "extra_tail_units": evidence.extra_tail_units,
        "audio_duration_seconds": evidence.audio_duration_seconds,
        "speaker_gate_applied": evidence.speaker_gate_applied,
        "speaker_similarity": evidence.speaker_similarity,
        "boundary_speaker_drop": evidence.boundary_speaker_drop,
        "pace_cps": evidence.pace_cps,
        "squim_gate_applied": evidence.squim_gate_applied,
        "squim_stoi": evidence.squim_stoi,
        "squim_pesq": evidence.squim_pesq,
        "squim_si_sdr": evidence.squim_si_sdr,
        "squim_quality_cost": evidence.squim_quality_cost,
        "score": evidence.score,
        "reasons": list(evidence.rejection_reasons),
    }


def _trajectory_gate_evidence_payload(
    evidence: TrajectoryGateEvidence | None,
) -> dict[str, Any] | None:
    if evidence is None:
        return None
    return {
        "passed": evidence.passed,
        "result_count": evidence.result_count,
        "score": evidence.score,
        "reasons": list(evidence.rejection_reasons),
        "result": (
            None
            if evidence.result is None
            else _candidate_gate_evidence_payload(evidence.result)
        ),
    }


def _local_independent_gate_evidence_payload(
    evidence: LocalIndependentGateEvidence,
) -> dict[str, Any]:
    return {
        "attempted": evidence.attempted,
        "passed": evidence.passed,
        "proof_count": evidence.proof_count,
        "result": (
            None
            if evidence.result is None
            else _candidate_gate_evidence_payload(evidence.result)
        ),
    }


def _candidate_attempt_evidence_payload(
    evidence: CandidateAttemptEvidence,
) -> dict[str, Any]:
    candidate_ordinals = evidence.chunk_candidate_ordinals
    chunk_policies = [
        generation_policy_for_candidate_offset(ordinal).name
        for ordinal in candidate_ordinals
    ]
    policy = (
        chunk_policies[0]
        if chunk_policies and len(set(chunk_policies)) == 1
        else generation_policy_for_candidate_offset(evidence.candidate_index).name
    )
    return {
        "candidate_index": evidence.candidate_index,
        "seed": evidence.seed,
        "policy": policy,
        "trajectory_passed": evidence.trajectory_passed,
        "trajectory_score": evidence.trajectory_score,
        "trajectory_reasons": list(
            evidence.trajectory_rejection_reasons
        ),
        "chunk_indices": list(evidence.chunk_indices),
        "chunk_text_units": list(evidence.chunk_text_units),
        "chunk_candidate_ordinals": list(candidate_ordinals),
        "chunk_policies": chunk_policies,
        "chunk_text_variants": list(evidence.chunk_text_variants),
        "network_conditioned": list(evidence.network_conditioned),
        "chunk_stop_reasons": list(evidence.chunk_stop_reasons),
        "chunk_endpoint_energy_ratios": list(
            evidence.chunk_endpoint_energy_ratios
        ),
        "chunk_generated_steps": list(evidence.chunk_generated_steps),
        "chunk_hard_stop_steps": list(evidence.chunk_hard_stop_steps),
        "scheduled_cfg": evidence.scheduled_cfg,
        "effective_cfgs": list(evidence.effective_cfgs),
        "floor_reasons": [list(reasons) for reasons in evidence.floor_reasons],
        "local_result_count": evidence.local_result_count,
        "local_results": [
            _candidate_gate_evidence_payload(result)
            for result in evidence.local_results
        ],
        "independent_local_evidence_complete": (
            len(evidence.independent_local_results)
            == evidence.local_result_count
        ),
        "independent_local_results": [
            _local_independent_gate_evidence_payload(result)
            for result in evidence.independent_local_results
        ],
        "joined_output": _trajectory_gate_evidence_payload(
            evidence.joined_output
        ),
        "independent_output": _trajectory_gate_evidence_payload(
            evidence.independent_output
        ),
    }


def _sequence_search_evidence_payload(
    evidence: SequenceSearchEvidence | None,
) -> dict[str, Any] | None:
    if evidence is None:
        return None
    eligible_counts = evidence.eligible_candidate_counts[
        :CASCADE_EVIDENCE_MAX_LOCAL_RESULTS
    ]
    transition_counts = evidence.finite_transition_counts[
        : max(0, CASCADE_EVIDENCE_MAX_LOCAL_RESULTS - 1)
    ]
    return {
        "row_count": len(evidence.eligible_candidate_counts),
        "eligible_candidate_counts": list(eligible_counts),
        "zero_eligible_rows": [
            index
            for index, count in enumerate(eligible_counts)
            if count == 0
        ],
        "transition_boundary_count": len(evidence.finite_transition_counts),
        "finite_transition_counts": list(transition_counts),
        "zero_transition_edges": [
            index
            for index, count in enumerate(transition_counts)
            if count == 0
        ],
        "ranked_path_count": evidence.ranked_path_count,
        "checked_path_count": len(evidence.checked_paths),
        "checked_paths": [
            {
                "rank": path.rank,
                "chunk_candidates": list(
                    path.chunk_candidate_indices[
                        :CASCADE_EVIDENCE_MAX_LOCAL_RESULTS
                    ]
                ),
                "chunk_seeds": list(
                    path.chunk_seeds[:CASCADE_EVIDENCE_MAX_LOCAL_RESULTS]
                ),
                "final_output": _trajectory_gate_evidence_payload(
                    path.final_output
                ),
            }
            for path in evidence.checked_paths[
                :CASCADE_EVIDENCE_MAX_SEQUENCE_PATHS
            ]
        ],
    }


def _selected_generation_evidence_payload(
    diagnostics: CascadeDiagnostics,
    selection: CascadeResult,
) -> dict[str, Any]:
    attempts = {
        attempt.candidate_index: attempt for attempt in diagnostics.attempts
    }
    scheduled_cfgs: list[float | None] = []
    effective_cfgs: list[float | None] = []
    floor_reasons: list[list[str]] = []
    candidate_ordinals: list[int | None] = []
    policies: list[str | None] = []
    network_conditioned: list[bool | None] = []
    chunk_text_variants: list[str | None] = []
    stop_reasons: list[str | None] = []
    endpoint_energy_ratios: list[float | None] = []
    generated_steps: list[int | None] = []
    hard_stop_steps: list[int | None] = []
    complete = True
    endpoint_complete = True
    for chunk_index, candidate_index in enumerate(
        selection.chunk_candidate_indices
    ):
        attempt = attempts.get(candidate_index)
        if attempt is None or attempt.scheduled_cfg is None:
            complete = False
            scheduled_cfgs.append(None)
            effective_cfgs.append(None)
            floor_reasons.append([])
            candidate_ordinals.append(None)
            policies.append(None)
            network_conditioned.append(None)
            chunk_text_variants.append(None)
            stop_reasons.append(None)
            endpoint_energy_ratios.append(None)
            generated_steps.append(None)
            hard_stop_steps.append(None)
            endpoint_complete = False
            continue
        try:
            local_index = attempt.chunk_indices.index(chunk_index)
            effective = attempt.effective_cfgs[local_index]
            reasons = attempt.floor_reasons[local_index]
        except (IndexError, ValueError):
            complete = False
            scheduled_cfgs.append(attempt.scheduled_cfg)
            effective_cfgs.append(None)
            floor_reasons.append([])
            candidate_ordinals.append(None)
            policies.append(None)
            network_conditioned.append(None)
            chunk_text_variants.append(None)
            stop_reasons.append(None)
            endpoint_energy_ratios.append(None)
            generated_steps.append(None)
            hard_stop_steps.append(None)
            endpoint_complete = False
            continue
        try:
            ordinal = attempt.chunk_candidate_ordinals[local_index]
        except IndexError:
            complete = False
            ordinal = None
        scheduled_cfgs.append(attempt.scheduled_cfg)
        effective_cfgs.append(effective)
        floor_reasons.append(list(reasons))
        candidate_ordinals.append(ordinal)
        policies.append(
            None
            if ordinal is None
            else generation_policy_for_candidate_offset(ordinal).name
        )
        try:
            network_conditioned.append(attempt.network_conditioned[local_index])
        except IndexError:
            complete = False
            network_conditioned.append(None)
        try:
            chunk_text_variants.append(attempt.chunk_text_variants[local_index])
        except IndexError:
            complete = False
            chunk_text_variants.append(None)
        try:
            stop_reason = attempt.chunk_stop_reasons[local_index]
            endpoint_energy_ratio = (
                attempt.chunk_endpoint_energy_ratios[local_index]
            )
            generated_step_count = attempt.chunk_generated_steps[local_index]
            hard_stop_step_count = attempt.chunk_hard_stop_steps[local_index]
        except IndexError:
            endpoint_complete = False
            stop_reasons.append(None)
            endpoint_energy_ratios.append(None)
            generated_steps.append(None)
            hard_stop_steps.append(None)
        else:
            endpoint_artifact = ChunkCandidateArtifact(
                stop_reason=stop_reason,
                endpoint_energy_ratio=endpoint_energy_ratio,
                generated_steps=generated_step_count,
                hard_stop_steps=hard_stop_step_count,
            )
            if (
                stop_reason is None
                or endpoint_energy_ratio is None
                or generated_step_count is None
                or hard_stop_step_count is None
                or not _endpoint_artifact_is_valid(endpoint_artifact)
            ):
                endpoint_complete = False
            stop_reasons.append(stop_reason)
            endpoint_energy_ratios.append(endpoint_energy_ratio)
            generated_steps.append(generated_step_count)
            hard_stop_steps.append(hard_stop_step_count)
    return {
        "complete": complete,
        "endpoint_complete": endpoint_complete,
        "chunk_scheduled_cfgs": scheduled_cfgs,
        "chunk_effective_cfgs": effective_cfgs,
        "chunk_floor_reasons": floor_reasons,
        "chunk_candidate_ordinals": candidate_ordinals,
        "chunk_policies": policies,
        "network_conditioned": network_conditioned,
        "chunk_text_variants": chunk_text_variants,
        "chunk_stop_reasons": stop_reasons,
        "chunk_endpoint_energy_ratios": endpoint_energy_ratios,
        "chunk_generated_steps": generated_steps,
        "chunk_hard_stop_steps": hard_stop_steps,
    }


def _attempt_endpoint_evidence_is_complete(
    attempt: CandidateAttemptEvidence,
) -> bool:
    """Validate every bounded endpoint row before declaring it complete."""

    row_count = len(attempt.chunk_indices)
    endpoint_rows = (
        attempt.chunk_stop_reasons,
        attempt.chunk_endpoint_energy_ratios,
        attempt.chunk_generated_steps,
        attempt.chunk_hard_stop_steps,
    )
    if row_count == 0 or any(len(row) != row_count for row in endpoint_rows):
        return False
    return all(
        reason is not None
        and energy is not None
        and generated is not None
        and hard_stop is not None
        and _endpoint_artifact_is_valid(
            ChunkCandidateArtifact(
                stop_reason=reason,
                endpoint_energy_ratio=energy,
                generated_steps=generated,
                hard_stop_steps=hard_stop,
            )
        )
        for reason, energy, generated, hard_stop in zip(
            *endpoint_rows,
            strict=True,
        )
    )


def format_cascade_evidence_log(
    diagnostics: CascadeDiagnostics,
    *,
    outcome: str,
    generated_chunk_limit: int,
    generated_text_unit_limit: int = CASCADE_EVIDENCE_MAX_TEXT_UNITS,
    selection: CascadeResult | None = None,
    final_output: TrajectoryGateEvidence | None = None,
    independent_final_output: TrajectoryGateEvidence | None = None,
) -> str:
    """Return one canonical JSON log line containing only bounded evidence."""

    if not isinstance(diagnostics, CascadeDiagnostics):
        raise TypeError("diagnostics must be CascadeDiagnostics")
    if outcome not in _CASCADE_EVIDENCE_OUTCOMES:
        raise ValueError("invalid cascade evidence outcome")
    if isinstance(generated_chunk_limit, (bool, np.bool_)):
        raise ValueError("generated_chunk_limit must be an integer between 1 and 32")
    try:
        limit = operator.index(generated_chunk_limit)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError(
            "generated_chunk_limit must be an integer between 1 and 32"
        ) from error
    if not 1 <= limit <= CASCADE_EVIDENCE_MAX_ATTEMPTS:
        raise ValueError("generated_chunk_limit must be an integer between 1 and 32")
    if isinstance(generated_text_unit_limit, (bool, np.bool_)):
        raise ValueError("generated_text_unit_limit must be an integer between 1 and 800")
    try:
        text_unit_limit = operator.index(generated_text_unit_limit)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError(
            "generated_text_unit_limit must be an integer between 1 and 800"
        ) from error
    if not 1 <= text_unit_limit <= CASCADE_EVIDENCE_MAX_TEXT_UNITS:
        raise ValueError(
            "generated_text_unit_limit must be an integer between 1 and 800"
        )

    attempts = diagnostics.attempts[:CASCADE_EVIDENCE_MAX_ATTEMPTS]
    for attempt in attempts:
        endpoint_rows = (
            attempt.chunk_stop_reasons,
            attempt.chunk_endpoint_energy_ratios,
            attempt.chunk_generated_steps,
            attempt.chunk_hard_stop_steps,
        )
        if any(endpoint_rows) and not _attempt_endpoint_evidence_is_complete(
            attempt
        ):
            raise ValueError("canonical endpoint evidence is invalid")

    selected: dict[str, Any] | None = None
    if selection is not None:
        if not isinstance(selection, CascadeResult):
            raise TypeError("selection must be a CascadeResult")
        mode = (
            selection.selection_mode
            if selection.selection_mode in _CASCADE_EVIDENCE_SELECTION_MODES
            else None
        )
        selected = {
            "selection": mode,
            "candidate_index": selection.candidate_index,
            "seed": selection.seed,
            "chunk_candidates": list(
                selection.chunk_candidate_indices[
                    :CASCADE_EVIDENCE_MAX_LOCAL_RESULTS
                ]
            ),
            "chunk_seeds": list(
                selection.chunk_seeds[:CASCADE_EVIDENCE_MAX_LOCAL_RESULTS]
            ),
            "sequence_rank": selection.sequence_path_rank,
            "sequence_paths_checked": selection.sequence_paths_checked,
            "generated_chunks": selection.generated_chunk_count,
            "generated_text_units": selection.generated_text_units,
            "chunk_candidate_counts": list(
                selection.chunk_candidate_counts[
                    :CASCADE_EVIDENCE_MAX_LOCAL_RESULTS
                ]
            ),
            "generation": _selected_generation_evidence_payload(
                diagnostics,
                selection,
            ),
        }

    generation_evidence_complete = bool(attempts) and all(
        attempt.scheduled_cfg is not None
        and len(attempt.chunk_indices) == len(attempt.chunk_text_units)
        == len(attempt.chunk_candidate_ordinals)
        == len(attempt.network_conditioned)
        == len(attempt.chunk_text_variants)
        == len(attempt.effective_cfgs)
        == len(attempt.floor_reasons)
        and bool(attempt.chunk_indices)
        for attempt in attempts
    )
    endpoint_evidence_complete = bool(attempts) and all(
        _attempt_endpoint_evidence_is_complete(attempt)
        for attempt in attempts
    )
    generated_chunk_count = sum(len(attempt.chunk_indices) for attempt in attempts)
    generated_text_units = sum(
        sum(attempt.chunk_text_units) for attempt in attempts
    )
    request_chunk_count = (
        len(attempts[0].chunk_indices) if attempts else 0
    )
    if selection is not None and generation_evidence_complete:
        if (
            selection.generated_chunk_count != generated_chunk_count
            or selection.generated_text_units != generated_text_units
        ):
            raise ValueError(
                "canonical generation evidence disagrees with cascade budgets"
            )
    payload = {
        "schema_version": CASCADE_EVIDENCE_SCHEMA_VERSION,
        "outcome": outcome,
        "request_chunk_count": request_chunk_count,
        "limits": {
            "generated_chunks": int(limit),
            "generated_text_units": int(text_unit_limit),
        },
        "cfg_contract": {
            "schedule": MIXED_CFG_SCHEDULE,
            "primary": MIXED_CFG_PRIMARY,
            "alternate": MIXED_CFG_ALTERNATE,
            "short_text_max_units": MIXED_CFG_SHORT_TEXT_MAX_UNITS,
            "short_text_min": MIXED_CFG_SHORT_TEXT_MIN,
            "network_min": MIXED_CFG_NETWORK_MIN,
        },
        "attempt_count": len(diagnostics.attempts),
        "generation_evidence_complete": generation_evidence_complete,
        "endpoint_evidence_complete": endpoint_evidence_complete,
        "generated_chunk_count": generated_chunk_count,
        "generated_text_units": generated_text_units,
        "attempts": [
            _candidate_attempt_evidence_payload(attempt)
            for attempt in attempts
        ],
        "sequence_search": _sequence_search_evidence_payload(
            diagnostics.sequence_search
        ),
        "selection": selected,
        "final_output": _trajectory_gate_evidence_payload(final_output),
        "independent_final_output": _trajectory_gate_evidence_payload(
            independent_final_output
        ),
    }
    return CASCADE_EVIDENCE_LOG_PREFIX + json.dumps(
        payload,
        allow_nan=False,
        ensure_ascii=True,
        separators=(",", ":"),
        sort_keys=True,
    )


def select_k_candidate_sequences(
    local_scores: Sequence[Sequence[float]],
    transition_scores: Sequence[Sequence[Sequence[float]]] = (),
    *,
    max_paths: int = 3,
) -> tuple[CandidateSequenceSelection, ...]:
    """Return up to three distinct finite paths in stable cost order.

    Each DP state retains only its ``max_paths`` best prefixes.  This keeps the
    search bounded at ``O(N K² max_paths)`` while still producing exact k-best
    paths for the requested small bound.  Single-chunk requests intentionally
    return no sequence fallback.
    """

    if isinstance(max_paths, (bool, np.bool_)):
        raise ValueError("max_paths must be an integer between 1 and 3")
    try:
        path_limit = operator.index(max_paths)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("max_paths must be an integer between 1 and 3") from error
    if not 1 <= path_limit <= 3:
        raise ValueError("max_paths must be an integer between 1 and 3")

    try:
        raw_local = [list(row) for row in local_scores]
    except TypeError:
        return ()
    if len(raw_local) <= 1 or any(not row for row in raw_local):
        return ()
    safe_local = [
        [
            score if score is not None else math.inf
            for score in (_finite_float(value, minimum=0.0) for value in row)
        ]
        for row in raw_local
    ]
    try:
        raw_transitions = [
            [list(row) for row in matrix]
            for matrix in transition_scores
        ]
    except TypeError:
        return ()
    if len(raw_transitions) != len(safe_local) - 1:
        return ()

    safe_transitions: list[list[list[float]]] = []
    for step, matrix in enumerate(raw_transitions):
        previous_count = len(safe_local[step])
        current_count = len(safe_local[step + 1])
        if len(matrix) != previous_count or any(
            len(row) != current_count for row in matrix
        ):
            return ()
        safe_transitions.append(
            [
                [
                    score if score is not None else math.inf
                    for score in (
                        _finite_float(value, minimum=0.0)
                        for value in row
                    )
                ]
                for row in matrix
            ]
        )

    # One list of (cost, path) prefixes for each current candidate position.
    states: list[list[tuple[float, tuple[int, ...]]]] = []
    for candidate_index, score in enumerate(safe_local[0]):
        states.append(
            [(score, (candidate_index,))] if math.isfinite(score) else []
        )

    for step in range(1, len(safe_local)):
        next_states: list[list[tuple[float, tuple[int, ...]]]] = []
        for current_index, local_score in enumerate(safe_local[step]):
            options: dict[tuple[int, ...], float] = {}
            if math.isfinite(local_score):
                for previous_index, prefixes in enumerate(states):
                    edge_score = safe_transitions[step - 1][previous_index][
                        current_index
                    ]
                    if not math.isfinite(edge_score):
                        continue
                    for previous_score, prefix in prefixes:
                        total = previous_score + edge_score + local_score
                        path = prefix + (current_index,)
                        if math.isfinite(total):
                            old_score = options.get(path, math.inf)
                            if total < old_score:
                                options[path] = total
            ranked = sorted(
                ((score, path) for path, score in options.items()),
                key=lambda item: (item[0], item[1]),
            )[:path_limit]
            next_states.append(ranked)
        states = next_states

    complete: dict[tuple[int, ...], float] = {}
    for prefixes in states:
        for score, path in prefixes:
            old_score = complete.get(path, math.inf)
            if score < old_score:
                complete[path] = score
    ranked_complete = sorted(
        ((score, path) for path, score in complete.items()),
        key=lambda item: (item[0], item[1]),
    )[:path_limit]
    return tuple(
        CandidateSequenceSelection(candidate_indices=path, total_score=score)
        for score, path in ranked_complete
    )


def select_culprit_diverse_candidate_sequences(
    local_scores: Sequence[Sequence[float]],
    transition_scores: Sequence[Sequence[Sequence[float]]] = (),
    *,
    culprit_indices: Sequence[int] = (),
    excluded_paths: Sequence[Sequence[int]] = (),
    max_paths: int = 3,
) -> tuple[CandidateSequenceSelection, ...]:
    """Select bounded low-cost paths with distinct culprit projections.

    The regular k-best result can spend all three exact-final checks on paths
    that differ only in already-stable chunks.  This helper adds the cheapest
    path forced through every finite row candidate, then prefers previously
    unseen assignments at the supplied culprit chunks.  It remains bounded by
    the total ragged candidate count (at most the generation-chunk budget).
    """

    if isinstance(max_paths, (bool, np.bool_)):
        raise ValueError("max_paths must be an integer between 1 and 3")
    try:
        path_limit = operator.index(max_paths)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("max_paths must be an integer between 1 and 3") from error
    if not 1 <= path_limit <= 3:
        raise ValueError("max_paths must be an integer between 1 and 3")
    try:
        raw_local = [list(row) for row in local_scores]
    except TypeError:
        return ()
    if not raw_local or any(not row for row in raw_local):
        return ()
    if len(raw_local) == 1:
        try:
            if list(transition_scores):
                return ()
        except TypeError:
            return ()

    culprit_rows: list[int] = []
    try:
        for value in culprit_indices:
            if isinstance(value, (bool, np.bool_)):
                return ()
            index = operator.index(value)
            if index < 0 or index >= len(raw_local):
                return ()
            if index not in culprit_rows:
                culprit_rows.append(index)
    except (TypeError, ValueError, OverflowError):
        return ()
    projection_rows = tuple(culprit_rows) or tuple(range(len(raw_local)))

    excluded: set[tuple[int, ...]] = set()
    try:
        for raw_path in excluded_paths:
            path_values = list(raw_path)
            if any(isinstance(value, (bool, np.bool_)) for value in path_values):
                return ()
            path = tuple(operator.index(value) for value in path_values)
            if len(path) != len(raw_local):
                return ()
            if any(
                index < 0 or index >= len(raw_local[row])
                for row, index in enumerate(path)
            ):
                return ()
            excluded.add(path)
    except (TypeError, ValueError, OverflowError):
        return ()

    candidates: dict[tuple[int, ...], float] = {}

    def retain(selection: CandidateSequenceSelection) -> None:
        path = tuple(selection.candidate_indices)
        score = _finite_float(selection.total_score, minimum=0.0)
        if len(path) != len(raw_local) or path in excluded or score is None:
            return
        old_score = candidates.get(path, math.inf)
        if score < old_score:
            candidates[path] = score

    if len(raw_local) == 1:
        for candidate_index, value in enumerate(raw_local[0]):
            score = _finite_float(value, minimum=0.0)
            if score is not None:
                retain(
                    CandidateSequenceSelection(
                        candidate_indices=(candidate_index,),
                        total_score=score,
                    )
                )
    else:
        for selection in select_k_candidate_sequences(
            raw_local,
            transition_scores,
            max_paths=path_limit,
        ):
            retain(selection)
        for row_index, row in enumerate(raw_local):
            for candidate_index, value in enumerate(row):
                if _finite_float(value, minimum=0.0) is None:
                    continue
                forced = [list(scores) for scores in raw_local]
                forced[row_index] = [
                    score if index == candidate_index else math.inf
                    for index, score in enumerate(forced[row_index])
                ]
                best = select_k_candidate_sequences(
                    forced,
                    transition_scores,
                    max_paths=1,
                )
                if best:
                    retain(best[0])

    ranked = sorted(
        (
            CandidateSequenceSelection(candidate_indices=path, total_score=score)
            for path, score in candidates.items()
        ),
        key=lambda selection: (
            selection.total_score,
            selection.candidate_indices,
        ),
    )
    if not ranked:
        return ()

    selected = [ranked.pop(0)]
    seen_projections = {
        tuple(selected[0].candidate_indices[index] for index in projection_rows)
    }
    while ranked and len(selected) < path_limit:
        diverse_index = next(
            (
                index
                for index, selection in enumerate(ranked)
                if tuple(
                    selection.candidate_indices[row] for row in projection_rows
                )
                not in seen_projections
            ),
            None,
        )
        selected_index = 0 if diverse_index is None else diverse_index
        selection = ranked.pop(selected_index)
        selected.append(selection)
        seen_projections.add(
            tuple(selection.candidate_indices[index] for index in projection_rows)
        )
    return tuple(selected)


def candidate_chunk_transition_score(
    previous_result: CandidateGateResult,
    previous_artifact: ChunkCandidateArtifact,
    current_result: CandidateGateResult,
    current_artifact: ChunkCandidateArtifact,
    *,
    speaker_weight: float = 1.0,
    rms_db_weight: float = 0.05,
    median_f0_weight: float = 0.10,
) -> float:
    """Return a finite adjacent-chunk cost or ``inf`` for an unsafe edge."""

    if previous_result.passed is not True or current_result.passed is not True:
        return math.inf
    speaker_w = _finite_float(speaker_weight, minimum=0.0)
    rms_w = _finite_float(rms_db_weight, minimum=0.0)
    f0_w = _finite_float(median_f0_weight, minimum=0.0)
    previous_rms = _finite_float(previous_artifact.rms_db)
    current_rms = _finite_float(current_artifact.rms_db)
    if None in (speaker_w, rms_w, f0_w, previous_rms, current_rms):
        return math.inf

    previous_embedding = previous_artifact.speaker_embedding
    current_embedding = current_artifact.speaker_embedding
    speaker_cost = 0.0
    if previous_result.speaker_gate_applied and previous_embedding is None:
        return math.inf
    if current_result.speaker_gate_applied and current_embedding is None:
        return math.inf
    if previous_embedding is not None and current_embedding is not None:
        try:
            speaker_cost = 1.0 - cosine_similarity(previous_embedding, current_embedding)
        except ValueError:
            return math.inf

    assert previous_rms is not None and current_rms is not None
    rms_cost = abs(previous_rms - current_rms)
    f0_cost = 0.0
    previous_f0 = previous_artifact.median_f0_hz
    current_f0 = current_artifact.median_f0_hz
    if previous_f0 is not None and current_f0 is not None:
        previous_pitch = _finite_float(previous_f0, minimum=1.0)
        current_pitch = _finite_float(current_f0, minimum=1.0)
        if previous_pitch is None or current_pitch is None:
            return math.inf
        f0_cost = abs(math.log2(current_pitch / previous_pitch))

    assert speaker_w is not None and rms_w is not None and f0_w is not None
    score = speaker_w * speaker_cost + rms_w * rms_cost + f0_w * f0_cost
    return score if math.isfinite(score) and score >= 0.0 else math.inf


def candidate_limit_for_chunk_budget(
    chunk_count: int,
    *,
    max_candidates: int = 32,
    max_generated_chunks: int = 32,
    total_text_units: int | None = None,
    max_generated_text_units: int | None = None,
) -> int:
    """Return a cap bounded by chunk count and optional text-generation work."""

    if isinstance(chunk_count, (bool, np.bool_)):
        raise ValueError("chunk_count must be a positive integer")
    try:
        chunks = int(chunk_count)
        candidates = int(max_candidates)
        generated_chunks = int(max_generated_chunks)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("candidate budget values must be integers") from error
    if chunks <= 0:
        raise ValueError("chunk_count must be a positive integer")
    if candidates <= 0 or candidates > ADAPTIVE_CASCADE_STAGE_LIMITS[-1]:
        raise ValueError("max_candidates must be between 1 and 32")
    if generated_chunks <= 0:
        raise ValueError("max_generated_chunks must be positive")
    if chunks > generated_chunks:
        raise ValueError("one trajectory exceeds the generated-chunk budget")
    limit = min(candidates, generated_chunks // chunks)
    if total_text_units is None and max_generated_text_units is None:
        return limit
    if total_text_units is None or max_generated_text_units is None:
        raise ValueError("text-unit budget fields must be provided together")
    if isinstance(total_text_units, (bool, np.bool_)) or isinstance(
        max_generated_text_units,
        (bool, np.bool_),
    ):
        raise ValueError("text-unit budget values must be integers")
    try:
        units = int(total_text_units)
        generated_units = int(max_generated_text_units)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("text-unit budget values must be integers") from error
    if units <= 0 or generated_units <= 0:
        raise ValueError("text-unit budget values must be positive")
    if units > generated_units:
        raise ValueError("one trajectory exceeds the generated-text-unit budget")
    return min(limit, generated_units // units)


@dataclass(frozen=True)
class _VerifiedTrajectoryCandidate:
    candidate_index: int
    seed: int
    trajectory: Any
    verification: TrajectoryGateResult
    independent_local_results: tuple[LocalIndependentGateEvidence, ...] = ()
    joined_output: TrajectoryGateEvidence | None = None
    independent_output: TrajectoryGateEvidence | None = None
    generation_evidence: CandidateGenerationEvidence | None = None


def _unwrap_candidate_verification(
    value: Any,
) -> tuple[
    TrajectoryGateResult,
    tuple[LocalIndependentGateEvidence, ...],
    TrajectoryGateEvidence | None,
    TrajectoryGateEvidence | None,
]:
    if isinstance(value, CandidateVerification):
        verification = value.verification
        independent_local_results = value.independent_local_results
        joined_output = value.joined_output
        independent_output = value.independent_output
    else:
        verification = value
        independent_local_results = ()
        joined_output = None
        independent_output = None
    if not isinstance(verification, TrajectoryGateResult):
        raise TypeError("candidate_verifier must return TrajectoryGateResult")
    if not isinstance(independent_local_results, tuple) or (
        independent_local_results
        and (
            len(independent_local_results)
            != len(verification.candidate_results)
            or any(
                not isinstance(result, LocalIndependentGateEvidence)
                or type(result.attempted) is not bool
                or type(result.proof_count) is not int
                or result.proof_count > CASCADE_EVIDENCE_MAX_LOCAL_RESULTS
                or (
                    result.attempted
                    and (
                        type(result.passed) is not bool
                        or result.proof_count <= 0
                        or not isinstance(result.result, CandidateGateEvidence)
                        or result.result.passed is not result.passed
                    )
                )
                or (
                    not result.attempted
                    and (
                        result.passed is not None
                        or result.proof_count < 0
                        or result.result is not None
                    )
                )
                for result in independent_local_results
            )
        )
    ):
        raise TypeError("candidate independent-local evidence is invalid")
    if joined_output is not None and not isinstance(
        joined_output,
        TrajectoryGateEvidence,
    ):
        raise TypeError("candidate joined-output evidence is invalid")
    if independent_output is not None and not isinstance(
        independent_output,
        TrajectoryGateEvidence,
    ):
        raise TypeError("candidate independent-output evidence is invalid")
    return (
        verification,
        independent_local_results,
        joined_output,
        independent_output,
    )


def _validated_candidate_generation_evidence(
    value: Any,
    *,
    chunk_indices: tuple[int, ...],
    chunks: tuple[str, ...],
    expected_candidate_ordinals: tuple[int, ...],
    require_explicit_candidate_ordinals: bool,
    require_endpoint_evidence: bool,
) -> CandidateGenerationEvidence:
    """Validate deterministic CFG evidence supplied by the hosted app."""

    if not isinstance(value, CandidateGenerationEvidence):
        raise TypeError(
            "generation evidence factory must return CandidateGenerationEvidence"
        )
    if value.chunk_indices != chunk_indices:
        raise ValueError("generation evidence chunk indices do not match the attempt")
    expected_units = tuple(count_speech_units(chunk) for chunk in chunks)
    if value.chunk_text_units != expected_units or any(unit <= 0 for unit in expected_units):
        raise ValueError("generation evidence text units do not match the attempt")
    if len(value.effective_cfgs) != len(chunks) or len(value.floor_reasons) != len(chunks):
        raise ValueError("generation evidence CFG rows do not match the attempt")
    if value.network_conditioned:
        if (
            not isinstance(value.network_conditioned, tuple)
            or len(value.network_conditioned) != len(chunks)
            or any(type(flag) is not bool for flag in value.network_conditioned)
        ):
            raise ValueError(
                "generation evidence network provenance does not match the attempt"
            )
        network_conditioned = value.network_conditioned
    else:
        network_conditioned = (False,) * len(chunks)
    if value.chunk_text_variants:
        if (
            not isinstance(value.chunk_text_variants, tuple)
            or len(value.chunk_text_variants) != len(chunks)
            or any(
                variant not in {"base", "email_domain_mail_v1"}
                for variant in value.chunk_text_variants
            )
        ):
            raise ValueError(
                "generation evidence text variants do not match the attempt"
            )
        chunk_text_variants = value.chunk_text_variants
    else:
        chunk_text_variants = ("base",) * len(chunks)
    endpoint_rows = (
        value.chunk_stop_reasons,
        value.chunk_endpoint_energy_ratios,
        value.chunk_generated_steps,
        value.chunk_hard_stop_steps,
    )
    endpoint_evidence_present = any(endpoint_rows)
    if require_endpoint_evidence and not all(endpoint_rows):
        raise ValueError("generation endpoint evidence is required")
    if endpoint_evidence_present and not all(
        isinstance(row, tuple) and len(row) == len(chunks)
        for row in endpoint_rows
    ):
        raise ValueError("generation endpoint evidence does not match the attempt")
    stop_reasons: list[str] = []
    endpoint_energy_ratios: list[float] = []
    generated_steps: list[int] = []
    hard_stop_steps: list[int] = []
    if endpoint_evidence_present:
        for raw_reason, raw_energy, raw_generated, raw_hard_stop in zip(
            *endpoint_rows,
            strict=True,
        ):
            if raw_reason not in _GENERATION_STOP_REASONS:
                raise ValueError("generation stop reason is invalid")
            energy = _finite_float(raw_energy, minimum=0.0, maximum=1.0)
            if energy is None:
                raise ValueError("generation endpoint energy is invalid")
            if any(
                isinstance(value, (bool, np.bool_))
                for value in (raw_generated, raw_hard_stop)
            ):
                raise ValueError("generation endpoint steps are invalid")
            try:
                generated = operator.index(raw_generated)
                hard_stop = operator.index(raw_hard_stop)
            except (TypeError, ValueError, OverflowError) as error:
                raise ValueError("generation endpoint steps are invalid") from error
            if (
                not 1 <= generated <= 2_000
                or not 1 <= hard_stop <= 2_000
                or generated > hard_stop
                or (raw_reason == "hard_stop" and generated != hard_stop)
            ):
                raise ValueError("generation endpoint steps are invalid")
            stop_reasons.append(raw_reason)
            endpoint_energy_ratios.append(energy)
            generated_steps.append(int(generated))
            hard_stop_steps.append(int(hard_stop))
    raw_ordinals = value.chunk_candidate_ordinals
    if not raw_ordinals:
        if require_explicit_candidate_ordinals:
            raise ValueError(
                "context-aware generation evidence must provide chunk candidate ordinals"
            )
        candidate_ordinals = expected_candidate_ordinals
    else:
        if not isinstance(raw_ordinals, tuple) or len(raw_ordinals) != len(chunks):
            raise ValueError("generation evidence candidate ordinals do not match the attempt")
        candidate_ordinals_list: list[int] = []
        for raw_ordinal in raw_ordinals:
            if isinstance(raw_ordinal, (bool, np.bool_)):
                raise ValueError("generation evidence candidate ordinal is invalid")
            try:
                ordinal = operator.index(raw_ordinal)
            except (TypeError, ValueError, OverflowError) as error:
                raise ValueError(
                    "generation evidence candidate ordinal is invalid"
                ) from error
            if ordinal < 0:
                raise ValueError("generation evidence candidate ordinal is invalid")
            candidate_ordinals_list.append(int(ordinal))
        candidate_ordinals = tuple(candidate_ordinals_list)
        if candidate_ordinals != expected_candidate_ordinals:
            raise ValueError(
                "generation evidence candidate ordinals do not match the schedule"
            )
    if len(set(candidate_ordinals)) != 1:
        raise ValueError("one generation call must use one candidate schedule ordinal")
    scheduled = _finite_float(value.scheduled_cfg, minimum=1.0, maximum=4.0)
    if scheduled is None:
        raise ValueError("generation evidence scheduled CFG is invalid")
    expected_scheduled = generation_cfg_for_candidate_offset(candidate_ordinals[0])
    if scheduled != expected_scheduled:
        raise ValueError("generation evidence scheduled CFG disagrees with the schedule")
    effective_cfgs: list[float] = []
    floor_reasons: list[tuple[str, ...]] = []
    for effective_value, raw_reasons, is_network in zip(
        value.effective_cfgs,
        value.floor_reasons,
        network_conditioned,
        strict=True,
    ):
        effective = _finite_float(effective_value, minimum=1.0, maximum=4.0)
        if effective is None or effective < scheduled:
            raise ValueError("generation evidence effective CFG is invalid")
        if not isinstance(raw_reasons, tuple) or any(
            reason not in _CFG_FLOOR_REASONS for reason in raw_reasons
        ):
            raise ValueError("generation evidence floor reasons are invalid")
        reasons = tuple(dict.fromkeys(raw_reasons))
        if len(reasons) != len(raw_reasons):
            raise ValueError("generation evidence floor reasons must be unique")
        if (not reasons and effective != scheduled) or (
            reasons and effective <= scheduled
        ):
            raise ValueError("generation evidence floor reasons disagree with CFG")
        network_floor_required = bool(
            is_network and scheduled < MIXED_CFG_NETWORK_MIN
        )
        if ("network" in reasons) != network_floor_required:
            raise ValueError(
                "generation evidence network floor disagrees with provenance"
            )
        if is_network and effective < MIXED_CFG_NETWORK_MIN:
            raise ValueError(
                "generation evidence network CFG is below the frozen minimum"
            )
        effective_cfgs.append(effective)
        floor_reasons.append(reasons)
    return CandidateGenerationEvidence(
        chunk_indices=chunk_indices,
        chunk_text_units=expected_units,
        scheduled_cfg=scheduled,
        effective_cfgs=tuple(effective_cfgs),
        floor_reasons=tuple(floor_reasons),
        chunk_candidate_ordinals=candidate_ordinals,
        network_conditioned=network_conditioned,
        chunk_text_variants=chunk_text_variants,
        chunk_stop_reasons=tuple(stop_reasons),
        chunk_endpoint_energy_ratios=tuple(endpoint_energy_ratios),
        chunk_generated_steps=tuple(generated_steps),
        chunk_hard_stop_steps=tuple(hard_stop_steps),
    )


def _candidate_attempt_evidence(
    candidate: _VerifiedTrajectoryCandidate,
) -> CandidateAttemptEvidence:
    verification = candidate.verification
    generation = candidate.generation_evidence
    local_results = verification.candidate_results[
        :CASCADE_EVIDENCE_MAX_LOCAL_RESULTS
    ]
    return CandidateAttemptEvidence(
        candidate_index=_evidence_int(candidate.candidate_index),
        seed=_evidence_int(
            candidate.seed,
            maximum=REQUEST_SEED_LIMIT + CASCADE_EVIDENCE_MAX_ATTEMPTS,
        ),
        trajectory_passed=verification.passed is True,
        trajectory_score=_evidence_float(verification.score),
        trajectory_rejection_reasons=_bounded_rejection_reasons(
            verification.rejection_reasons
        ),
        local_result_count=_evidence_int(len(verification.candidate_results)),
        local_results=tuple(
            candidate_gate_evidence(result)
            for result in local_results
            if isinstance(result, CandidateGateResult)
        ),
        chunk_indices=(generation.chunk_indices if generation is not None else ()),
        chunk_text_units=(
            generation.chunk_text_units if generation is not None else ()
        ),
        chunk_candidate_ordinals=(
            generation.chunk_candidate_ordinals if generation is not None else ()
        ),
        network_conditioned=(
            generation.network_conditioned if generation is not None else ()
        ),
        chunk_text_variants=(
            generation.chunk_text_variants if generation is not None else ()
        ),
        chunk_stop_reasons=(
            generation.chunk_stop_reasons if generation is not None else ()
        ),
        chunk_endpoint_energy_ratios=(
            generation.chunk_endpoint_energy_ratios
            if generation is not None
            else ()
        ),
        chunk_generated_steps=(
            generation.chunk_generated_steps if generation is not None else ()
        ),
        chunk_hard_stop_steps=(
            generation.chunk_hard_stop_steps if generation is not None else ()
        ),
        scheduled_cfg=(generation.scheduled_cfg if generation is not None else None),
        effective_cfgs=(generation.effective_cfgs if generation is not None else ()),
        floor_reasons=(generation.floor_reasons if generation is not None else ()),
        independent_local_results=candidate.independent_local_results,
        joined_output=candidate.joined_output,
        independent_output=candidate.independent_output,
    )


def _cascade_diagnostics(
    candidates: Sequence[_VerifiedTrajectoryCandidate],
    sequence_search: SequenceSearchEvidence | None = None,
) -> CascadeDiagnostics:
    return CascadeDiagnostics(
        attempts=tuple(
            _candidate_attempt_evidence(candidate)
            for candidate in tuple(candidates)[:CASCADE_EVIDENCE_MAX_ATTEMPTS]
        ),
        sequence_search=sequence_search,
    )


@dataclass(frozen=True)
class _RaggedChunkCandidate:
    """One locally gated chunk retained in a coverage-adaptive pool."""

    candidate_index: int
    seed: int
    audio: Any
    result: CandidateGateResult
    artifact: ChunkCandidateArtifact


def _whole_trajectory_result(
    candidate: _VerifiedTrajectoryCandidate,
    attempted_seeds: Sequence[int],
    chunk_count: int,
    *,
    diagnostics: CascadeDiagnostics,
) -> CascadeResult:
    return CascadeResult(
        trajectory=candidate.trajectory,
        verification=candidate.verification,
        seed=candidate.seed,
        candidate_index=candidate.candidate_index,
        attempted_seeds=tuple(attempted_seeds),
        chunk_candidate_indices=(candidate.candidate_index,) * chunk_count,
        chunk_seeds=(candidate.seed,) * chunk_count,
        selection_mode="whole_trajectory",
        diagnostics=diagnostics,
    )


@dataclass(frozen=True)
class _SequenceFallbackSearchResult:
    results: tuple[CascadeResult, ...]
    evidence: SequenceSearchEvidence


def _sequence_fallback_search(
    candidates: Sequence[_VerifiedTrajectoryCandidate],
    attempted_seeds: Sequence[int],
    chunk_count: int,
    *,
    max_paths: int,
    max_local_boundary_speaker_drop: float | None = None,
) -> _SequenceFallbackSearchResult:
    """Rank mixed-seed paths using safe or narrowly recoverable local chunks.

    A boundary-only local rejection may be made eligible up to the supplied
    fallback cap.  This is intentionally an internal DP representation: the
    caller must still verify the exactly assembled mixed path with the stricter
    joined/final gate before any audio can be returned.
    """

    if not candidates:
        return _SequenceFallbackSearchResult(
            results=(),
            evidence=SequenceSearchEvidence(
                eligible_candidate_counts=(0,) * max(0, chunk_count),
                finite_transition_counts=(0,) * max(0, chunk_count - 1),
                ranked_path_count=0,
            ),
        )
    if chunk_count <= 1:
        eligible = 0
        if chunk_count == 1:
            for candidate in candidates:
                results = candidate.verification.candidate_results
                if len(results) != 1:
                    continue
                result = results[0]
                if (
                    isinstance(result, CandidateGateResult)
                    and result.passed is True
                    and math.isfinite(result.score)
                    and result.score >= 0.0
                ):
                    eligible += 1
        return _SequenceFallbackSearchResult(
            results=(),
            evidence=SequenceSearchEvidence(
                eligible_candidate_counts=((eligible,) if chunk_count == 1 else ()),
                finite_transition_counts=(),
                ranked_path_count=0,
            ),
        )
    local_scores: list[list[float]] = [[] for _ in range(chunk_count)]
    usable: list[bool] = []
    sequence_results: list[tuple[CandidateGateResult, ...]] = []
    for candidate in candidates:
        verification = candidate.verification
        try:
            trajectory_length = len(candidate.trajectory)
        except TypeError:
            trajectory_length = -1
        candidate_usable = bool(
            trajectory_length == chunk_count
            and len(verification.candidate_results) == chunk_count
            and len(verification.chunk_artifacts) == chunk_count
        )
        usable.append(candidate_usable)
        adjusted_results: list[CandidateGateResult] = []
        for chunk_index in range(chunk_count):
            score = math.inf
            if candidate_usable:
                result = verification.candidate_results[chunk_index]
                artifact = verification.chunk_artifacts[chunk_index]
                adjusted_result = _sequence_fallback_candidate_result(
                    result,
                    max_local_boundary_speaker_drop=max_local_boundary_speaker_drop,
                )
                adjusted_results.append(adjusted_result)
                if (
                    adjusted_result.passed
                    and math.isfinite(adjusted_result.score)
                    and adjusted_result.score >= 0.0
                ):
                    speaker_artifact_valid = True
                    if adjusted_result.speaker_gate_applied:
                        try:
                            speaker_artifact_valid = bool(
                                artifact.speaker_embedding is not None
                                and cosine_similarity(
                                    artifact.speaker_embedding,
                                    artifact.speaker_embedding,
                                ) >= 1.0 - 1.0e-6
                            )
                        except ValueError:
                            speaker_artifact_valid = False
                    if speaker_artifact_valid:
                        endpoint_cost = candidate_endpoint_selection_cost(
                            artifact
                        )
                        adjusted_score = adjusted_result.score + endpoint_cost
                        if math.isfinite(adjusted_score):
                            adjusted_result = replace(
                                adjusted_result,
                                score=adjusted_score,
                            )
                            adjusted_results[-1] = adjusted_result
                            score = adjusted_score
            local_scores[chunk_index].append(score)
        sequence_results.append(tuple(adjusted_results))

    transitions: list[list[list[float]]] = []
    for chunk_index in range(1, chunk_count):
        matrix: list[list[float]] = []
        for previous_position, previous_candidate in enumerate(candidates):
            row: list[float] = []
            for current_position, current_candidate in enumerate(candidates):
                score = math.inf
                if usable[previous_position] and usable[current_position]:
                    score = candidate_chunk_transition_score(
                        sequence_results[previous_position][chunk_index - 1],
                        previous_candidate.verification.chunk_artifacts[chunk_index - 1],
                        sequence_results[current_position][chunk_index],
                        current_candidate.verification.chunk_artifacts[chunk_index],
                    )
                row.append(score)
            matrix.append(row)
        transitions.append(matrix)

    selections = select_k_candidate_sequences(
        local_scores,
        transitions,
        max_paths=max_paths,
    )
    output: list[CascadeResult] = []
    for rank, selection in enumerate(selections, 1):
        selected_candidates = tuple(
            candidates[position]
            for position in selection.candidate_indices
        )
        selected_trajectory = tuple(
            candidate.trajectory[chunk_index]
            for chunk_index, candidate in enumerate(selected_candidates)
        )
        selected_results = tuple(
            sequence_results[position][chunk_index]
            for chunk_index, position in enumerate(selection.candidate_indices)
        )
        selected_artifacts = tuple(
            candidate.verification.chunk_artifacts[chunk_index]
            for chunk_index, candidate in enumerate(selected_candidates)
        )
        verification = TrajectoryGateResult(
            passed=True,
            candidate_results=selected_results,
            score=selection.total_score,
            rejection_reasons=(),
            chunk_artifacts=selected_artifacts,
        )
        output.append(
            CascadeResult(
                trajectory=selected_trajectory,
                verification=verification,
                seed=None,
                candidate_index=None,
                attempted_seeds=tuple(attempted_seeds),
                chunk_candidate_indices=tuple(
                    candidate.candidate_index for candidate in selected_candidates
                ),
                chunk_seeds=tuple(
                    candidate.seed for candidate in selected_candidates
                ),
                selection_mode="sequence_dp",
                sequence_path_rank=rank,
            )
        )
    results = tuple(output)
    evidence = SequenceSearchEvidence(
        eligible_candidate_counts=tuple(
            sum(math.isfinite(score) for score in row)
            for row in local_scores
        ),
        finite_transition_counts=tuple(
            sum(
                math.isfinite(score)
                for row in matrix
                for score in row
            )
            for matrix in transitions
        ),
        ranked_path_count=len(results),
    )
    return _SequenceFallbackSearchResult(results=results, evidence=evidence)


def _sequence_fallback_results(
    candidates: Sequence[_VerifiedTrajectoryCandidate],
    attempted_seeds: Sequence[int],
    chunk_count: int,
    *,
    max_paths: int,
    max_local_boundary_speaker_drop: float | None = None,
) -> tuple[CascadeResult, ...]:
    """Compatibility wrapper returning only ranked sequence results."""

    return _sequence_fallback_search(
        candidates,
        attempted_seeds,
        chunk_count,
        max_paths=max_paths,
        max_local_boundary_speaker_drop=max_local_boundary_speaker_drop,
    ).results


def _sequence_fallback_candidate_result(
    result: CandidateGateResult,
    *,
    max_local_boundary_speaker_drop: float | None,
) -> CandidateGateResult:
    """Return a DP-safe view of one local result or an unchanged hard reject."""

    if result.passed is True and math.isfinite(result.score) and result.score >= 0.0:
        return result
    limit = (
        None
        if max_local_boundary_speaker_drop is None
        else _finite_float(
            max_local_boundary_speaker_drop,
            minimum=0.0,
            maximum=1.0,
        )
    )
    if (
        limit is None
        or result.rejection_reasons != ("boundary_speaker_drop",)
        or result.speaker_gate_applied is not True
        or result.comparison.passed is not True
    ):
        return result
    similarity = _finite_float(
        result.speaker_similarity,
        minimum=-1.0,
        maximum=1.0,
    )
    boundary_drop = _finite_float(result.boundary_speaker_drop, minimum=0.0)
    cer = _finite_float(result.comparison.cer, minimum=0.0)
    if (
        similarity is None
        or boundary_drop is None
        or boundary_drop > limit + 1.0e-12
        or cer is None
    ):
        return result
    score = (
        cer
        + SEQUENCE_FALLBACK_SPEAKER_WEIGHT * (1.0 - similarity)
        + SEQUENCE_FALLBACK_BOUNDARY_WEIGHT * boundary_drop
    )
    if not math.isfinite(score) or score < 0.0:
        return result
    return replace(
        result,
        passed=True,
        score=score,
        rejection_reasons=(),
    )


def local_candidate_has_coverage_eligibility(
    result: CandidateGateResult,
    *,
    max_local_boundary_speaker_drop: float | None,
) -> bool:
    """Return whether a primary local result could enter either safe pool."""

    if not isinstance(result, CandidateGateResult):
        return False
    eligible = _sequence_fallback_candidate_result(
        result,
        max_local_boundary_speaker_drop=max_local_boundary_speaker_drop,
    )
    return bool(
        eligible.passed is True
        and math.isfinite(eligible.score)
        and eligible.score >= 0.0
        and not eligible.rejection_reasons
    )


def _preferred_speaker_verification(
    verification: TrajectoryGateResult,
    *,
    min_similarity: float,
    max_boundary_drop: float,
) -> bool:
    if verification.passed is not True or not math.isfinite(verification.score):
        return False
    for result in verification.candidate_results:
        if not result.speaker_gate_applied:
            continue
        similarity = _finite_float(result.speaker_similarity, minimum=-1.0, maximum=1.0)
        boundary_drop = _finite_float(result.boundary_speaker_drop, minimum=0.0)
        if (
            similarity is None
            or boundary_drop is None
            or similarity < min_similarity
            or boundary_drop > max_boundary_drop
        ):
            return False
    return True


def _preferred_squim_verification(
    verification: TrajectoryGateResult,
    *,
    min_stoi: float,
    min_pesq: float,
    min_audio_duration_seconds: float = 0.0,
) -> bool:
    """Require long-enough hard-gated chunks to meet the preferred tier."""

    preferred_stoi = _finite_float(min_stoi, minimum=0.0, maximum=1.0)
    preferred_pesq = _finite_float(min_pesq, minimum=0.0, maximum=5.0)
    duration_floor = _finite_float(min_audio_duration_seconds, minimum=0.0)
    if (
        preferred_stoi is None
        or preferred_pesq is None
        or duration_floor is None
        or verification.passed is not True
        or not math.isfinite(verification.score)
    ):
        return False
    for result in verification.candidate_results:
        duration = _finite_float(result.audio_duration_seconds, minimum=0.0)
        if duration is None:
            return False
        if duration < duration_floor:
            continue
        if result.squim_gate_applied is not True:
            return False
        stoi = _finite_float(result.squim_stoi, minimum=0.0, maximum=1.0)
        pesq = _finite_float(result.squim_pesq, minimum=0.0, maximum=5.0)
        if (
            stoi is None
            or pesq is None
            or stoi < preferred_stoi
            or pesq < preferred_pesq
        ):
            return False
    return True


def _preferred_release_verification(
    verification: TrajectoryGateResult,
    *,
    min_speaker_similarity: float | None,
    max_boundary_speaker_drop: float | None,
    min_squim_stoi: float | None,
    min_squim_pesq: float | None,
    min_squim_audio_duration_seconds: float = 0.0,
) -> bool:
    """Combine independently optional preferred speaker and quality tiers."""

    if min_speaker_similarity is not None:
        assert max_boundary_speaker_drop is not None
        if not _preferred_speaker_verification(
            verification,
            min_similarity=min_speaker_similarity,
            max_boundary_drop=max_boundary_speaker_drop,
        ):
            return False
    if min_squim_stoi is not None:
        assert min_squim_pesq is not None
        if not _preferred_squim_verification(
            verification,
            min_stoi=min_squim_stoi,
            min_pesq=min_squim_pesq,
            min_audio_duration_seconds=min_squim_audio_duration_seconds,
        ):
            return False
    return verification.passed is True and math.isfinite(verification.score)


def _preferred_release_evidence(
    evidence: TrajectoryGateEvidence | None,
    *,
    min_speaker_similarity: float | None,
    max_boundary_speaker_drop: float | None,
    min_squim_stoi: float | None,
    min_squim_pesq: float | None,
    min_squim_audio_duration_seconds: float = 0.0,
) -> bool:
    """Apply the preferred tier to one exact joined-waveform snapshot."""

    if (
        not isinstance(evidence, TrajectoryGateEvidence)
        or evidence.passed is not True
        or evidence.result_count != 1
        or evidence.result is None
        or evidence.result.passed is not True
        or _finite_float(evidence.score, minimum=0.0) is None
    ):
        return False
    result = evidence.result
    if min_speaker_similarity is not None:
        assert max_boundary_speaker_drop is not None
        if result.speaker_gate_applied:
            similarity = _finite_float(
                result.speaker_similarity,
                minimum=-1.0,
                maximum=1.0,
            )
            boundary_drop = _finite_float(
                result.boundary_speaker_drop,
                minimum=0.0,
            )
            if (
                similarity is None
                or boundary_drop is None
                or similarity < min_speaker_similarity
                or boundary_drop > max_boundary_speaker_drop
            ):
                return False
    if min_squim_stoi is not None:
        assert min_squim_pesq is not None
        duration = _finite_float(
            result.audio_duration_seconds,
            minimum=0.0,
        )
        duration_floor = _finite_float(
            min_squim_audio_duration_seconds,
            minimum=0.0,
        )
        if duration is None or duration_floor is None:
            return False
        if duration >= duration_floor:
            stoi = _finite_float(
                result.squim_stoi,
                minimum=0.0,
                maximum=1.0,
            )
            pesq = _finite_float(
                result.squim_pesq,
                minimum=0.0,
                maximum=5.0,
            )
            if (
                result.squim_gate_applied is not True
                or stoi is None
                or pesq is None
                or stoi < min_squim_stoi
                or pesq < min_squim_pesq
            ):
                return False
    return True


def _single_exact_fast_release_evidence(
    release_evidence: TrajectoryGateEvidence | None,
    independent_evidence: TrajectoryGateEvidence | None,
    *,
    min_speaker_similarity: float,
    max_boundary_speaker_drop: float,
    min_squim_stoi: float,
    min_squim_pesq: float,
    min_audio_duration_seconds: float,
) -> bool:
    """Accept only a fully measured exact waveform at the fast quality tier.

    Unlike the general preferred helper, this latency shortcut never treats a
    skipped short-audio speaker or SQUIM gate as success. It is deliberately
    limited to exact joined-waveform evidence; local chunk proxies cannot
    trigger it.
    """

    if (
        not isinstance(release_evidence, TrajectoryGateEvidence)
        or release_evidence.result is None
        or release_evidence.result.speaker_gate_applied is not True
        or release_evidence.result.squim_gate_applied is not True
        or not _preferred_release_evidence(
            independent_evidence,
            min_speaker_similarity=None,
            max_boundary_speaker_drop=None,
            min_squim_stoi=None,
            min_squim_pesq=None,
        )
    ):
        return False
    duration = _finite_float(
        release_evidence.result.audio_duration_seconds,
        minimum=0.0,
    )
    if duration is None or duration < min_audio_duration_seconds:
        return False
    return _preferred_release_evidence(
        release_evidence,
        min_speaker_similarity=min_speaker_similarity,
        max_boundary_speaker_drop=max_boundary_speaker_drop,
        min_squim_stoi=min_squim_stoi,
        min_squim_pesq=min_squim_pesq,
        min_squim_audio_duration_seconds=min_audio_duration_seconds,
    )


def _coverage_trajectory_tuple(trajectory: Any, expected_chunks: int) -> tuple[Any, ...]:
    """Return one generator result as an exact, non-string trajectory."""

    if isinstance(trajectory, (str, bytes, bytearray, np.ndarray)):
        raise RuntimeError("candidate generator returned a malformed trajectory")
    try:
        normalized = tuple(trajectory)
    except TypeError as error:
        raise RuntimeError("candidate generator returned a malformed trajectory") from error
    if len(normalized) != expected_chunks:
        raise RuntimeError("candidate generator returned a misaligned trajectory")
    return normalized


def _coverage_score_is_valid(value: Any, *, finite: bool) -> bool:
    """Return whether a gate score is non-negative and non-NaN."""

    if isinstance(value, (bool, np.bool_)):
        return False
    try:
        score = float(value)
    except (TypeError, ValueError, OverflowError):
        return False
    if math.isnan(score) or score < 0.0:
        return False
    return math.isfinite(score) if finite else score != -math.inf


def _validate_coverage_evidence(
    verification: Any,
    expected_chunks: int,
    *,
    verifier_name: str,
) -> TrajectoryGateResult:
    """Validate structural evidence before retaining any local chunk."""

    if not isinstance(verification, TrajectoryGateResult):
        raise RuntimeError(f"{verifier_name} returned an invalid result")
    if not isinstance(verification.passed, (bool, np.bool_)):
        raise RuntimeError(f"{verifier_name} returned an invalid pass flag")
    if not all(
        isinstance(value, tuple)
        for value in (
            verification.candidate_results,
            verification.chunk_artifacts,
            verification.rejection_reasons,
        )
    ):
        raise RuntimeError(f"{verifier_name} returned malformed local evidence")
    try:
        results = tuple(verification.candidate_results)
        artifacts = tuple(verification.chunk_artifacts)
        reasons = tuple(verification.rejection_reasons)
    except TypeError as error:
        raise RuntimeError(
            f"{verifier_name} returned malformed local evidence"
        ) from error
    if (
        len(results) != expected_chunks
        or len(artifacts) != expected_chunks
        or any(
            not isinstance(result, CandidateGateResult)
            or not isinstance(result.comparison, AsrComparison)
            or not isinstance(result.passed, (bool, np.bool_))
            or not isinstance(result.speaker_gate_applied, (bool, np.bool_))
            or not isinstance(result.comparison.passed, (bool, np.bool_))
            or not _coverage_score_is_valid(result.score, finite=result.passed is True)
            or not isinstance(result.rejection_reasons, tuple)
            or any(
                not isinstance(reason, str)
                for reason in result.rejection_reasons
            )
            for result in results
        )
        or any(
            not isinstance(artifact, ChunkCandidateArtifact)
            for artifact in artifacts
        )
        or any(not isinstance(reason, str) for reason in reasons)
        or not _coverage_score_is_valid(
            verification.score,
            finite=verification.passed is True,
        )
    ):
        raise RuntimeError(f"{verifier_name} returned malformed local evidence")
    if verification.passed is True and (
        reasons
        or any(
            result.passed is not True
            or result.rejection_reasons
            or result.comparison.passed is not True
            for result in results
        )
    ):
        raise RuntimeError(f"{verifier_name} returned inconsistent passing evidence")
    if verification.passed is True and any(
        not _coverage_artifact_is_valid(result, artifact)
        for result, artifact in zip(
            results,
            artifacts,
            strict=True,
        )
    ):
        raise RuntimeError(f"{verifier_name} passed without valid acoustic evidence")
    return verification


def _endpoint_artifact_is_valid(artifact: ChunkCandidateArtifact) -> bool:
    """Validate an optional all-or-none endpoint provenance row."""

    endpoint_values = (
        artifact.stop_reason,
        artifact.endpoint_energy_ratio,
        artifact.generated_steps,
        artifact.hard_stop_steps,
    )
    if all(value is None for value in endpoint_values):
        return True
    if any(value is not None for value in endpoint_values):
        if any(value is None for value in endpoint_values):
            return False
        if artifact.stop_reason not in _GENERATION_STOP_REASONS:
            return False
        energy = _finite_float(
            artifact.endpoint_energy_ratio,
            minimum=0.0,
            maximum=1.0,
        )
        if energy is None or any(
            isinstance(value, (bool, np.bool_))
            for value in (artifact.generated_steps, artifact.hard_stop_steps)
        ):
            return False
        try:
            generated = operator.index(artifact.generated_steps)
            hard_stop = operator.index(artifact.hard_stop_steps)
        except (TypeError, ValueError, OverflowError):
            return False
        if (
            not 1 <= generated <= 2_000
            or not 1 <= hard_stop <= 2_000
            or generated > hard_stop
            or (
                artifact.stop_reason == "hard_stop"
                and generated != hard_stop
            )
        ):
            return False
    return True


def _coverage_artifact_is_valid(
    result: CandidateGateResult,
    artifact: ChunkCandidateArtifact,
) -> bool:
    """Require finite transition evidence and ECAPA evidence when gated."""

    if result.comparison.passed is not True:
        return False
    if result.squim_gate_applied and any(
        value is None
        for value in (
            _finite_float(result.squim_stoi, minimum=0.0, maximum=1.0),
            _finite_float(result.squim_pesq, minimum=0.0, maximum=5.0),
            _finite_float(result.squim_si_sdr),
            _finite_float(result.squim_quality_cost, minimum=0.0),
        )
    ):
        return False
    if _finite_float(artifact.rms_db) is None:
        return False
    if artifact.median_f0_hz is not None and _finite_float(
        artifact.median_f0_hz,
        minimum=1.0,
    ) is None:
        return False
    if not _endpoint_artifact_is_valid(artifact):
        return False
    embedding = artifact.speaker_embedding
    if result.speaker_gate_applied is True and embedding is None:
        return False
    if embedding is not None:
        try:
            if cosine_similarity(embedding, embedding) < 1.0 - 1.0e-6:
                return False
        except ValueError:
            return False
    return True


def _bind_generation_endpoint_evidence(
    verification: TrajectoryGateResult,
    generation: CandidateGenerationEvidence | None,
    *,
    required: bool,
) -> TrajectoryGateResult:
    """Attach validated generation endpoint provenance to chunk artifacts."""

    if generation is None or not generation.chunk_stop_reasons:
        if required:
            raise RuntimeError("candidate endpoint evidence is missing")
        return verification
    row_count = len(verification.chunk_artifacts)
    endpoint_rows = (
        generation.chunk_stop_reasons,
        generation.chunk_endpoint_energy_ratios,
        generation.chunk_generated_steps,
        generation.chunk_hard_stop_steps,
    )
    if any(len(row) != row_count for row in endpoint_rows):
        raise RuntimeError("candidate endpoint evidence is misaligned")
    artifacts = tuple(
        replace(
            artifact,
            stop_reason=reason,
            endpoint_energy_ratio=energy,
            generated_steps=generated,
            hard_stop_steps=hard_stop,
        )
        for artifact, reason, energy, generated, hard_stop in zip(
            verification.chunk_artifacts,
            *endpoint_rows,
            strict=True,
        )
    )
    if any(
        not _coverage_artifact_is_valid(result, artifact)
        for result, artifact in zip(
            verification.candidate_results,
            artifacts,
            strict=True,
        )
        if result.passed is True
    ):
        raise RuntimeError("candidate endpoint evidence is invalid")
    return replace(verification, chunk_artifacts=artifacts)


def candidate_endpoint_selection_cost(
    artifact: ChunkCandidateArtifact,
) -> float:
    """Return a bounded soft preference for natural, quiet endpoints."""

    if artifact.stop_reason is None and artifact.endpoint_energy_ratio is None:
        return 0.0
    if not _endpoint_artifact_is_valid(artifact):
        return math.inf
    energy = float(artifact.endpoint_energy_ratio)
    hard_stop = (
        ENDPOINT_HARD_STOP_PENALTY
        if _endpoint_artifact_reached_hard_cap(artifact)
        else 0.0
    )
    return hard_stop + ENDPOINT_ENERGY_WEIGHT * energy


def _endpoint_artifact_reached_hard_cap(
    artifact: ChunkCandidateArtifact,
) -> bool:
    """Return whether endpoint evidence reached the configured hard cap.

    A threshold crossing can coincide with the final permitted generation
    step.  The stop reason correctly records that the model crossed the
    threshold, but endpoint selection must still treat it as cap-reached so it
    cannot evade the bounded hard-cap cost or natural-endpoint waiver rules.
    """

    if not _endpoint_artifact_is_valid(artifact):
        return False
    if artifact.generated_steps is None or artifact.hard_stop_steps is None:
        return False
    return artifact.generated_steps >= artifact.hard_stop_steps


def _preferred_natural_endpoint_waiver(
    verification: TrajectoryGateResult,
    artifact: ChunkCandidateArtifact,
    *,
    min_speaker_similarity: float | None,
    max_boundary_speaker_drop: float | None,
    min_squim_stoi: float | None,
    min_squim_pesq: float | None,
    min_squim_audio_duration_seconds: float,
) -> bool:
    """Allow only a tightly bounded quality trade for a true pre-cap stop."""

    if (
        artifact.stop_reason != "stop_threshold"
        or _endpoint_artifact_reached_hard_cap(artifact)
    ):
        return False
    relaxed_stoi = (
        None
        if min_squim_stoi is None
        else max(0.0, min_squim_stoi - ENDPOINT_PREFERRED_SQUIM_STOI_SLACK)
    )
    relaxed_pesq = (
        None
        if min_squim_pesq is None
        else max(0.0, min_squim_pesq - ENDPOINT_PREFERRED_SQUIM_PESQ_SLACK)
    )
    return _preferred_release_verification(
        verification,
        min_speaker_similarity=min_speaker_similarity,
        max_boundary_speaker_drop=max_boundary_speaker_drop,
        min_squim_stoi=relaxed_stoi,
        min_squim_pesq=relaxed_pesq,
        min_squim_audio_duration_seconds=min_squim_audio_duration_seconds,
    )


def _coverage_local_candidate(
    *,
    candidate_index: int,
    seed: int,
    audio: Any,
    result: CandidateGateResult,
    artifact: ChunkCandidateArtifact,
    max_local_boundary_speaker_drop: float | None,
) -> _RaggedChunkCandidate | None:
    """Retain only a strict pass or the frozen boundary-only DP exception."""

    adjusted = _sequence_fallback_candidate_result(
        result,
        max_local_boundary_speaker_drop=max_local_boundary_speaker_drop,
    )
    if (
        adjusted.passed is not True
        or not math.isfinite(adjusted.score)
        or adjusted.score < 0.0
        or adjusted.rejection_reasons
    ):
        return None
    if not _coverage_artifact_is_valid(adjusted, artifact):
        raise RuntimeError("local verifier passed without valid acoustic evidence")
    endpoint_cost = candidate_endpoint_selection_cost(artifact)
    adjusted_score = adjusted.score + endpoint_cost
    if not math.isfinite(adjusted_score) or adjusted_score < 0.0:
        raise RuntimeError("local verifier passed without valid endpoint evidence")
    adjusted = replace(adjusted, score=adjusted_score)
    return _RaggedChunkCandidate(
        candidate_index=candidate_index,
        seed=seed,
        audio=audio,
        result=adjusted,
        artifact=artifact,
    )


def _coverage_final_passed(verification: Any) -> bool:
    """Accept only one internally consistent exact whole-waveform result."""

    if not isinstance(verification, TrajectoryGateResult):
        raise RuntimeError("sequence final verifier returned an invalid result")
    if not all(
        isinstance(value, tuple)
        for value in (
            verification.candidate_results,
            verification.rejection_reasons,
        )
    ):
        raise RuntimeError("sequence final verifier returned malformed evidence")
    if (
        len(verification.candidate_results) != 1
        or not isinstance(verification.candidate_results[0], CandidateGateResult)
        or not isinstance(verification.candidate_results[0].comparison, AsrComparison)
        or not isinstance(verification.passed, (bool, np.bool_))
        or any(not isinstance(reason, str) for reason in verification.rejection_reasons)
    ):
        raise RuntimeError("sequence final verifier returned malformed evidence")
    result = verification.candidate_results[0]
    if (
        not isinstance(result.passed, (bool, np.bool_))
        or not isinstance(result.comparison.passed, (bool, np.bool_))
        or not isinstance(result.rejection_reasons, tuple)
        or any(not isinstance(reason, str) for reason in result.rejection_reasons)
        or not _coverage_score_is_valid(
            verification.score,
            finite=verification.passed is True,
        )
        or not _coverage_score_is_valid(result.score, finite=result.passed is True)
    ):
        raise RuntimeError("sequence final verifier returned malformed evidence")
    return bool(
        verification.passed is True
        and not verification.rejection_reasons
        and result.passed is True
        and not result.rejection_reasons
        and result.comparison.passed is True
    )


def run_coverage_adaptive_cascade(
    chunks: Sequence[str],
    root_seed: int,
    candidate_generator: Callable[..., Any],
    whole_candidate_verifier: (
        Callable[
            [Any, tuple[str, ...], int],
            TrajectoryGateResult | CandidateVerification,
        ]
    ),
    refill_candidate_verifier: (
        Callable[
            [Any, tuple[str, ...], int],
            TrajectoryGateResult | CandidateVerification,
        ]
    ),
    *,
    sequence_final_verifier: Callable[
        [CascadeResult, tuple[str, ...]],
        TrajectoryGateResult,
    ],
    generation_evidence_factory: Callable[..., CandidateGenerationEvidence] | None = None,
    candidate_generation_text_transform: (
        Callable[
            [tuple[str, ...], CandidateGenerationContext],
            Sequence[str],
        ]
        | None
    ) = None,
    transition_artifact_enricher: (
        Callable[[Any, ChunkCandidateArtifact], ChunkCandidateArtifact] | None
    ) = None,
    max_generated_chunks: int = 32,
    max_generated_text_units: int = 800,
    max_sequence_paths: int = 3,
    sequence_fallback_max_local_boundary_speaker_drop: float | None = None,
    preferred_min_speaker_similarity: float | None = None,
    preferred_max_boundary_speaker_drop: float | None = None,
    preferred_min_squim_stoi: float | None = None,
    preferred_min_squim_pesq: float | None = None,
    preferred_min_squim_audio_duration_seconds: float = 0.0,
    single_exact_min_speaker_similarity: float | None = None,
    single_exact_max_boundary_speaker_drop: float | None = None,
    require_endpoint_evidence: bool = False,
) -> CascadeResult:
    """Run one whole trajectory, then deterministic low-coverage refills.

    The initial same-seed trajectory is the only full-trajectory generation.
    If its exact whole-output checks fail, every valid local observation is
    retained.  Later seeds generate exactly one low-coverage chunk under hard
    generated-chunk and generated-text-unit budgets.  Ragged DP paths are
    never returned without the supplied exact whole-waveform verifier.

    Existing two-argument generation callbacks remain unchanged.  A callback
    that explicitly accepts the keyword-only ``generation_context`` opts into
    per-chunk refill scheduling.  Its evidence factory must accept the same
    keyword and report the supplied row-local candidate ordinals.

    ``candidate_generation_text_transform`` may derive candidate-local text
    from the canonical verifier targets and immutable generation context.  The
    transformed chunks are sent only to the generator and generation-evidence
    factory.  Semantic verifiers continue to receive the canonical chunks.
    Generated-text-unit accounting uses the transformed text and is checked
    before every generator call.

    ``transition_artifact_enricher`` may lazily attach expensive soft
    transition evidence to retained candidates. It runs only when the ragged
    lattice contains more than one selectable path. A one-path lattice has no
    ranking decision, so enriching it cannot affect the selected waveform and
    is deliberately skipped.
    """

    if not all(
        callable(callback)
        for callback in (
            candidate_generator,
            whole_candidate_verifier,
            refill_candidate_verifier,
            sequence_final_verifier,
        )
    ):
        raise ValueError("coverage-adaptive callbacks must be callable")
    if generation_evidence_factory is not None and not callable(
        generation_evidence_factory
    ):
        raise ValueError("generation_evidence_factory must be callable")
    if type(require_endpoint_evidence) is not bool:
        raise ValueError("require_endpoint_evidence must be a boolean")
    if require_endpoint_evidence and generation_evidence_factory is None:
        raise ValueError(
            "endpoint evidence requires a generation evidence factory"
        )
    if candidate_generation_text_transform is not None and not callable(
        candidate_generation_text_transform
    ):
        raise ValueError("candidate_generation_text_transform must be callable")
    if (
        transition_artifact_enricher is not None
        and not callable(transition_artifact_enricher)
    ):
        raise ValueError("transition artifact enricher must be callable")

    def accepts_generation_context(callback: Callable[..., Any]) -> bool:
        try:
            parameters = inspect.signature(callback).parameters.values()
        except (TypeError, ValueError):
            return False
        return any(
            parameter.kind is inspect.Parameter.VAR_KEYWORD
            or (
                parameter.name == "generation_context"
                and parameter.kind
                in (
                    inspect.Parameter.POSITIONAL_OR_KEYWORD,
                    inspect.Parameter.KEYWORD_ONLY,
                )
            )
            for parameter in parameters
        )

    context_aware_generator = accepts_generation_context(candidate_generator)
    context_aware_evidence = (
        generation_evidence_factory is not None
        and accepts_generation_context(generation_evidence_factory)
    )
    if context_aware_generator != context_aware_evidence:
        raise ValueError(
            "context-aware generation and evidence callbacks must opt in together"
        )

    def generation_chunks(
        canonical_chunks: tuple[str, ...],
        context: CandidateGenerationContext,
    ) -> tuple[str, ...]:
        if candidate_generation_text_transform is None:
            return canonical_chunks
        try:
            transformed = candidate_generation_text_transform(
                canonical_chunks,
                context,
            )
        except Exception as error:
            raise RuntimeError(
                "candidate generation text transform failed"
            ) from error
        if isinstance(transformed, (str, bytes, bytearray, np.ndarray)):
            raise RuntimeError(
                "candidate generation text transform returned invalid chunks"
            )
        try:
            normalized = tuple(transformed)
        except TypeError as error:
            raise RuntimeError(
                "candidate generation text transform returned invalid chunks"
            ) from error
        if (
            len(normalized) != len(canonical_chunks)
            or any(
                not isinstance(chunk, str)
                or not chunk
                or count_speech_units(chunk) <= 0
                for chunk in normalized
            )
        ):
            raise RuntimeError(
                "candidate generation text transform returned invalid chunks"
            )
        return normalized

    try:
        chunk_tuple = tuple(str(chunk) for chunk in chunks)
    except TypeError as error:
        raise ValueError("coverage-adaptive cascade requires text chunks") from error
    if not chunk_tuple or any(not chunk for chunk in chunk_tuple):
        raise ValueError("coverage-adaptive cascade requires non-empty text chunks")
    if isinstance(root_seed, (bool, np.bool_)):
        raise ValueError("root_seed must be an integer")
    try:
        base_seed = operator.index(root_seed)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("root_seed must be an integer") from error

    def positive_budget(value: Any, name: str) -> int:
        if isinstance(value, (bool, np.bool_)):
            raise ValueError(f"{name} must be a positive integer")
        try:
            normalized = operator.index(value)
        except (TypeError, ValueError, OverflowError) as error:
            raise ValueError(f"{name} must be a positive integer") from error
        if normalized <= 0:
            raise ValueError(f"{name} must be a positive integer")
        return normalized

    chunk_budget = positive_budget(max_generated_chunks, "max_generated_chunks")
    text_budget = positive_budget(
        max_generated_text_units,
        "max_generated_text_units",
    )
    if chunk_budget > ADAPTIVE_CASCADE_STAGE_LIMITS[-1]:
        raise ValueError("max_generated_chunks cannot exceed the frozen cap of 32")
    if text_budget > 800:
        raise ValueError(
            "max_generated_text_units cannot exceed the frozen cap of 800"
        )
    path_limit = positive_budget(max_sequence_paths, "max_sequence_paths")
    if path_limit > 3:
        raise ValueError("max_sequence_paths must be between 1 and 3")
    boundary_limit = None
    if sequence_fallback_max_local_boundary_speaker_drop is not None:
        boundary_limit = _finite_float(
            sequence_fallback_max_local_boundary_speaker_drop,
            minimum=0.0,
            maximum=SEQUENCE_FALLBACK_MAX_LOCAL_BOUNDARY_SPEAKER_DROP,
        )
        if boundary_limit is None:
            raise ValueError(
                "sequence fallback boundary threshold must be finite and no greater than 0.15"
            )

    preferred_speaker_enabled = (
        preferred_min_speaker_similarity is not None
        or preferred_max_boundary_speaker_drop is not None
    )
    if preferred_speaker_enabled:
        preferred_similarity = _finite_float(
            preferred_min_speaker_similarity,
            minimum=-1.0,
            maximum=1.0,
        )
        preferred_boundary = _finite_float(
            preferred_max_boundary_speaker_drop,
            minimum=0.0,
            maximum=1.0,
        )
        if preferred_similarity is None or preferred_boundary is None:
            raise ValueError(
                "preferred speaker thresholds must be supplied together and finite"
            )
    else:
        preferred_similarity = None
        preferred_boundary = None
    preferred_squim_enabled = (
        preferred_min_squim_stoi is not None
        or preferred_min_squim_pesq is not None
    )
    if preferred_squim_enabled:
        preferred_stoi = _finite_float(
            preferred_min_squim_stoi,
            minimum=0.0,
            maximum=1.0,
        )
        preferred_pesq = _finite_float(
            preferred_min_squim_pesq,
            minimum=0.0,
            maximum=5.0,
        )
        preferred_squim_duration = _finite_float(
            preferred_min_squim_audio_duration_seconds,
            minimum=0.0,
        )
        if (
            preferred_stoi is None
            or preferred_pesq is None
            or preferred_squim_duration is None
        ):
            raise ValueError(
                "preferred SQUIM thresholds and duration must be finite"
            )
    else:
        preferred_stoi = None
        preferred_pesq = None
        preferred_squim_duration = 0.0
    single_exact_fast_enabled = (
        single_exact_min_speaker_similarity is not None
        or single_exact_max_boundary_speaker_drop is not None
    )
    if single_exact_fast_enabled:
        single_exact_similarity = _finite_float(
            single_exact_min_speaker_similarity,
            minimum=-1.0,
            maximum=1.0,
        )
        single_exact_boundary = _finite_float(
            single_exact_max_boundary_speaker_drop,
            minimum=0.0,
            maximum=1.0,
        )
        if single_exact_similarity is None or single_exact_boundary is None:
            raise ValueError(
                "single exact speaker thresholds must be supplied together and finite"
            )
        if not preferred_squim_enabled:
            raise ValueError(
                "single exact fast tier requires preferred SQUIM thresholds"
            )
    else:
        single_exact_similarity = None
        single_exact_boundary = None
    preferred_single_search = bool(
        preferred_speaker_enabled or preferred_squim_enabled
    )

    chunk_units = tuple(count_speech_units(chunk) for chunk in chunk_tuple)
    chunk_count = len(chunk_tuple)
    # A single local chunk still receives a fresh exact whole-waveform speaker
    # measurement before return.  Allow the same narrowly bounded local proxy
    # used by multi-chunk DP; the published release boundary remains unchanged.
    dp_boundary_limit = boundary_limit
    if chunk_count > chunk_budget:
        raise ValueError("initial trajectory exceeds the generated-chunk budget")
    initial_context = CandidateGenerationContext(
        candidate_index=0,
        seed=base_seed,
        chunk_indices=tuple(range(chunk_count)),
        chunk_candidate_ordinals=(0,) * chunk_count,
    )
    initial_generation_chunks = generation_chunks(
        chunk_tuple,
        initial_context,
    )
    initial_units = sum(
        count_speech_units(chunk) for chunk in initial_generation_chunks
    )
    if initial_units > text_budget:
        raise ValueError("initial trajectory exceeds the generated-text-unit budget")

    attempted_seeds: list[int] = []
    diagnostic_candidates: list[_VerifiedTrajectoryCandidate] = []
    generated_chunks = chunk_count
    generated_units = initial_units

    def generation_evidence(
        context: CandidateGenerationContext,
        candidate_chunks: tuple[str, ...],
    ) -> CandidateGenerationEvidence | None:
        if generation_evidence_factory is None:
            return None
        try:
            if context_aware_evidence:
                raw_evidence = generation_evidence_factory(
                    context.candidate_index,
                    context.seed,
                    context.chunk_indices,
                    candidate_chunks,
                    generation_context=context,
                )
                expected_ordinals = context.chunk_candidate_ordinals
            else:
                raw_evidence = generation_evidence_factory(
                    context.candidate_index,
                    context.seed,
                    context.chunk_indices,
                    candidate_chunks,
                )
                expected_ordinals = (context.candidate_index,) * len(
                    candidate_chunks
                )
            return _validated_candidate_generation_evidence(
                raw_evidence,
                chunk_indices=context.chunk_indices,
                chunks=candidate_chunks,
                expected_candidate_ordinals=expected_ordinals,
                require_explicit_candidate_ordinals=context_aware_evidence,
                require_endpoint_evidence=require_endpoint_evidence,
            )
        except Exception as error:
            raise RuntimeError("candidate generation evidence is invalid") from error

    try:
        if context_aware_generator:
            raw_initial_trajectory = candidate_generator(
                initial_generation_chunks,
                base_seed,
                generation_context=initial_context,
            )
        else:
            raw_initial_trajectory = candidate_generator(
                initial_generation_chunks,
                base_seed,
            )
    except Exception as error:
        raise RuntimeError("initial trajectory generation failed") from error
    initial_trajectory = _coverage_trajectory_tuple(
        raw_initial_trajectory,
        chunk_count,
    )
    initial_generation_evidence = generation_evidence(
        initial_context,
        initial_generation_chunks,
    )
    attempted_seeds.append(base_seed)
    try:
        raw_initial_verification = whole_candidate_verifier(
            initial_trajectory,
            chunk_tuple,
            base_seed,
        )
    except Exception as error:
        raise RuntimeError("whole trajectory verification failed") from error
    try:
        (
            initial_verification,
            initial_independent_local_results,
            initial_joined_output,
            initial_independent_output,
        ) = (
            _unwrap_candidate_verification(raw_initial_verification)
        )
    except TypeError as error:
        raise RuntimeError(
            "whole candidate verifier returned an invalid result"
        ) from error
    initial_verification = _validate_coverage_evidence(
        initial_verification,
        chunk_count,
        verifier_name="whole candidate verifier",
    )
    initial_verification = _bind_generation_endpoint_evidence(
        initial_verification,
        initial_generation_evidence,
        required=require_endpoint_evidence,
    )
    diagnostic_candidates.append(
        _VerifiedTrajectoryCandidate(
            candidate_index=0,
            seed=base_seed,
            trajectory=initial_trajectory,
            verification=initial_verification,
            independent_local_results=initial_independent_local_results,
            joined_output=initial_joined_output,
            independent_output=initial_independent_output,
            generation_evidence=initial_generation_evidence,
        )
    )
    initial_preferred = (
        _preferred_release_verification(
            initial_verification,
            min_speaker_similarity=preferred_similarity,
            max_boundary_speaker_drop=preferred_boundary,
            min_squim_stoi=preferred_stoi,
            min_squim_pesq=preferred_pesq,
            min_squim_audio_duration_seconds=preferred_squim_duration,
        )
        or _preferred_release_evidence(
            initial_joined_output,
            min_speaker_similarity=preferred_similarity,
            max_boundary_speaker_drop=preferred_boundary,
            min_squim_stoi=preferred_stoi,
            min_squim_pesq=preferred_pesq,
            min_squim_audio_duration_seconds=preferred_squim_duration,
        )
    )
    initial_fast_exact = bool(
        chunk_count == 1
        and single_exact_fast_enabled
        and _single_exact_fast_release_evidence(
            initial_joined_output,
            initial_independent_output,
            min_speaker_similarity=single_exact_similarity,
            max_boundary_speaker_drop=single_exact_boundary,
            min_squim_stoi=preferred_stoi,
            min_squim_pesq=preferred_pesq,
            min_audio_duration_seconds=preferred_squim_duration,
        )
    )
    if initial_verification.passed is True and (
        chunk_count > 1
        or not preferred_single_search
        or initial_preferred
        or initial_fast_exact
    ):
        return CascadeResult(
            trajectory=initial_trajectory,
            verification=initial_verification,
            seed=base_seed,
            candidate_index=0,
            attempted_seeds=(base_seed,),
            chunk_candidate_indices=(0,) * chunk_count,
            chunk_seeds=(base_seed,) * chunk_count,
            selection_mode="whole_trajectory",
            diagnostics=_cascade_diagnostics(diagnostic_candidates),
            generated_chunk_count=generated_chunks,
            generated_text_units=generated_units,
            chunk_candidate_counts=(1,) * chunk_count,
        )

    pools: list[list[_RaggedChunkCandidate]] = [
        [] for _ in range(chunk_count)
    ]
    initial_culprits: list[int] = []
    initial_all_strict = True
    for chunk_index, (audio, result, artifact) in enumerate(
        zip(
            initial_trajectory,
            initial_verification.candidate_results,
            initial_verification.chunk_artifacts,
            strict=True,
        )
    ):
        strict_pass = bool(
            result.passed is True
            and math.isfinite(result.score)
            and result.score >= 0.0
            and not result.rejection_reasons
        )
        initial_all_strict = initial_all_strict and strict_pass
        if not strict_pass:
            initial_culprits.append(chunk_index)
        retained = None
        if not (
            chunk_count == 1
            and initial_joined_output is not None
            and initial_verification.passed is not True
        ):
            retained = _coverage_local_candidate(
                candidate_index=0,
                seed=base_seed,
                audio=audio,
                result=result,
                artifact=artifact,
                max_local_boundary_speaker_drop=dp_boundary_limit,
            )
        if retained is not None:
            pools[chunk_index].append(retained)

    refill_attempts = [0] * chunk_count
    latest_local_results = list(initial_verification.candidate_results)
    next_candidate_index = 1
    sequence_path_ledger: list[SequencePathEvidence] = []
    checked_sequence_path_identities: set[tuple[int, ...]] = set()
    eager_hard_pass_result: CascadeResult | None = None
    enriched_transition_candidates: set[tuple[int, int]] = set()
    # Keep one exact-path check in reserve for the completed lattice. With the
    # production K=3 contract this permits the original first complete-path
    # probe plus one incremental preferred-tier rescue after a new candidate
    # arrives, without increasing the final-verifier budget.
    incremental_probe_limit = max(1, path_limit - 1)

    def enrich_transition_artifacts_for_ranking() -> None:
        """Attach soft transition evidence once, immediately before ranking."""

        if transition_artifact_enricher is None:
            return
        for chunk_index, pool in enumerate(pools):
            for position, candidate in enumerate(pool):
                identity = (chunk_index, candidate.candidate_index)
                if identity in enriched_transition_candidates:
                    continue
                try:
                    artifact = transition_artifact_enricher(
                        candidate.audio,
                        candidate.artifact,
                    )
                except Exception as error:
                    raise RuntimeError(
                        "transition artifact enrichment failed"
                    ) from error
                if not isinstance(artifact, ChunkCandidateArtifact):
                    raise RuntimeError(
                        "transition artifact enricher returned invalid evidence"
                    )
                raw_median_f0_hz = artifact.median_f0_hz
                median_f0_hz: float | None = None
                if raw_median_f0_hz is not None:
                    median_f0_hz = _finite_float(
                        raw_median_f0_hz,
                        minimum=1.0,
                    )
                    if median_f0_hz is None:
                        raise RuntimeError(
                            "transition artifact enricher returned invalid evidence"
                        )
                # The callback may supply only the soft F0 statistic. Keep all
                # semantic, speaker, RMS and endpoint provenance from the
                # already validated retained row; it must never be able to
                # rewrite evidence that was bound before ranking.
                enriched_artifact = replace(
                    candidate.artifact,
                    median_f0_hz=median_f0_hz,
                )
                if not _coverage_artifact_is_valid(
                    candidate.result,
                    enriched_artifact,
                ):
                    raise RuntimeError(
                        "transition artifact enricher returned invalid evidence"
                    )
                pool[position] = replace(
                    candidate,
                    artifact=enriched_artifact,
                )
                enriched_transition_candidates.add(identity)

    def eager_rank_one_sequence_probe() -> CascadeResult | None:
        """Verify a bounded new rank-one path before filling every row to K=3.

        One final-path slot remains reserved for the completed lattice. Each
        incremental probe excludes paths already checked. A hard failure keeps
        the remaining verifier budget for the completed lattice; only a hard
        pass that misses the preferred tier may trigger one early alternative.
        The probe returns only when both the unchanged hard final gate and
        preferred tier pass.
        """

        nonlocal eager_hard_pass_result
        if (
            len(sequence_path_ledger) >= incremental_probe_limit
            or (
                sequence_path_ledger
                and eager_hard_pass_result is None
            )
            or chunk_count <= 1
            or not preferred_single_search
            or any(not pool for pool in pools)
        ):
            return None

        # With one candidate per row there is only one possible waveform.
        # Soft transition evidence cannot change its rank and is intentionally
        # deferred. Once any row has an alternative, enrich the full lattice
        # before computing even the first transition score.
        if any(len(pool) > 1 for pool in pools):
            enrich_transition_artifacts_for_ranking()

        local_scores = [
            [candidate.result.score for candidate in pool]
            for pool in pools
        ]
        transitions = [
            [
                [
                    candidate_chunk_transition_score(
                        previous.result,
                        previous.artifact,
                        current.result,
                        current.artifact,
                    )
                    for current in pools[chunk_index]
                ]
                for previous in pools[chunk_index - 1]
            ]
            for chunk_index in range(1, chunk_count)
        ]
        excluded_path_list: list[tuple[int, ...]] = []
        if (
            initial_verification.passed is not True
            and initial_all_strict
            and all(pool[0].candidate_index == 0 for pool in pools)
        ):
            excluded_path_list.append((0,) * chunk_count)
        for checked_identity in sorted(checked_sequence_path_identities):
            checked_positions = tuple(
                next(
                    position
                    for position, candidate in enumerate(pools[chunk_index])
                    if candidate.candidate_index == candidate_index
                )
                for chunk_index, candidate_index in enumerate(
                    checked_identity
                )
            )
            if checked_positions not in excluded_path_list:
                excluded_path_list.append(checked_positions)
        selections = select_culprit_diverse_candidate_sequences(
            local_scores,
            transitions,
            culprit_indices=tuple(initial_culprits),
            excluded_paths=tuple(excluded_path_list),
            max_paths=1,
        )
        # An all-strict initial assembly may be the only path and is excluded
        # because its exact output was already rejected.  Keep the probe armed
        # until the first genuinely new rank-one path becomes available.
        if not selections:
            return None

        selection = selections[0]
        selected = tuple(
            pools[chunk_index][position]
            for chunk_index, position in enumerate(selection.candidate_indices)
        )
        local_verification = TrajectoryGateResult(
            passed=True,
            candidate_results=tuple(candidate.result for candidate in selected),
            score=selection.total_score,
            rejection_reasons=(),
            chunk_artifacts=tuple(candidate.artifact for candidate in selected),
        )
        eligible_counts = tuple(len(pool) for pool in pools)
        finite_transition_counts = tuple(
            sum(
                math.isfinite(score)
                for row in matrix
                for score in row
            )
            for matrix in transitions
        )
        sequence_result = CascadeResult(
            trajectory=tuple(candidate.audio for candidate in selected),
            verification=local_verification,
            seed=None,
            candidate_index=None,
            attempted_seeds=tuple(attempted_seeds),
            chunk_candidate_indices=tuple(
                candidate.candidate_index for candidate in selected
            ),
            chunk_seeds=tuple(candidate.seed for candidate in selected),
            selection_mode="coverage_sequence_dp",
            sequence_path_rank=len(sequence_path_ledger) + 1,
            diagnostics=_cascade_diagnostics(
                diagnostic_candidates,
                SequenceSearchEvidence(
                    eligible_candidate_counts=eligible_counts,
                    finite_transition_counts=finite_transition_counts,
                    ranked_path_count=len(sequence_path_ledger) + 1,
                ),
            ),
            generated_chunk_count=generated_chunks,
            generated_text_units=generated_units,
            chunk_candidate_counts=eligible_counts,
        )
        try:
            final_verification = sequence_final_verifier(
                sequence_result,
                chunk_tuple,
            )
        except Exception as error:
            raise RuntimeError(
                "sequence final verification failed; refusing unverified audio"
            ) from error
        final_passed = _coverage_final_passed(final_verification)
        final_evidence = trajectory_gate_evidence(final_verification)
        path_identity = sequence_result.chunk_candidate_indices
        checked_sequence_path_identities.add(path_identity)
        sequence_path_ledger.append(
            SequencePathEvidence(
                rank=len(sequence_path_ledger) + 1,
                chunk_candidate_indices=path_identity,
                chunk_seeds=sequence_result.chunk_seeds,
                final_output=final_evidence,
            )
        )
        preferred_passed = _preferred_release_verification(
            final_verification,
            min_speaker_similarity=preferred_similarity,
            max_boundary_speaker_drop=preferred_boundary,
            min_squim_stoi=preferred_stoi,
            min_squim_pesq=preferred_pesq,
            min_squim_audio_duration_seconds=preferred_squim_duration,
        )
        if final_passed and eager_hard_pass_result is None:
            eager_hard_pass_result = sequence_result
        if not (final_passed and preferred_passed):
            return None

        sequence_search = SequenceSearchEvidence(
            eligible_candidate_counts=eligible_counts,
            finite_transition_counts=finite_transition_counts,
            ranked_path_count=len(sequence_path_ledger),
            checked_paths=tuple(sequence_path_ledger),
        )
        return replace(
            sequence_result,
            sequence_paths_checked=len(sequence_path_ledger),
            diagnostics=_cascade_diagnostics(
                diagnostic_candidates,
                sequence_search,
            ),
        )

    eager_result = eager_rank_one_sequence_probe()
    if eager_result is not None:
        return eager_result

    while generated_chunks < chunk_budget:
        if candidate_generation_text_transform is None and any(
            not pool and generated_units + chunk_units[index] > text_budget
            for index, pool in enumerate(pools)
        ):
            break
        has_noninitial_coverage = any(
            candidate.candidate_index > 0
            for pool in pools
            for candidate in pool
        )
        needs_first_alternative = initial_all_strict and not has_noninitial_coverage
        structurally_eligible = [
            index
            for index, pool in enumerate(pools)
            if (
                (chunk_count == 1 and preferred_single_search)
                or len(pool) < path_limit
                or needs_first_alternative
            )
        ]
        if candidate_generation_text_transform is None:
            eligible = [
                index
                for index in structurally_eligible
                if generated_units + chunk_units[index] <= text_budget
            ]
        else:
            eligible = structurally_eligible
        if not eligible:
            break

        all_rows_covered = all(pools)

        def refill_priority(index: int) -> tuple[int, int, int, int, int]:
            # Once every row has safe local coverage, spend any remaining
            # bounded budget on request endpoints first.  The exact final
            # speaker gate compares the beginning and ending thirds, so an
            # extra interior candidate cannot repair endpoint identity drift.
            endpoint_rank = (
                0
                if all_rows_covered and index in {0, chunk_count - 1}
                else 1
            )
            primary_exact_retry_rank = int(
                bool(pools[index])
                or latest_local_results[index].comparison.passed is not True
            )
            return (
                endpoint_rank,
                len(pools[index]),
                primary_exact_retry_rank,
                refill_attempts[index],
                index,
            )

        refill_proposal = None
        for proposed_index in sorted(eligible, key=refill_priority):
            proposed_seed = base_seed + next_candidate_index
            proposed_canonical_chunks = (chunk_tuple[proposed_index],)
            proposed_context = CandidateGenerationContext(
                candidate_index=next_candidate_index,
                seed=proposed_seed,
                chunk_indices=(proposed_index,),
                chunk_candidate_ordinals=(
                    refill_attempts[proposed_index] + 1,
                ),
            )
            proposed_generation_chunks = generation_chunks(
                proposed_canonical_chunks,
                proposed_context,
            )
            proposed_units = sum(
                count_speech_units(chunk)
                for chunk in proposed_generation_chunks
            )
            if generated_units + proposed_units <= text_budget:
                refill_proposal = (
                    proposed_index,
                    proposed_seed,
                    proposed_generation_chunks,
                    proposed_context,
                    proposed_units,
                )
                break
        if refill_proposal is None:
            break
        (
            chunk_index,
            seed,
            refill_generation_chunks,
            refill_context,
            refill_units,
        ) = refill_proposal
        refill_chunks = (chunk_tuple[chunk_index],)
        if generated_chunks + len(refill_generation_chunks) > chunk_budget:
            break
        try:
            if context_aware_generator:
                raw_refill_trajectory = candidate_generator(
                    refill_generation_chunks,
                    seed,
                    generation_context=refill_context,
                )
            else:
                raw_refill_trajectory = candidate_generator(
                    refill_generation_chunks,
                    seed,
                )
        except Exception as error:
            raise RuntimeError("chunk refill generation failed") from error
        refill_trajectory = _coverage_trajectory_tuple(raw_refill_trajectory, 1)
        refill_generation_evidence = generation_evidence(
            refill_context,
            refill_generation_chunks,
        )
        attempted_seeds.append(seed)
        generated_chunks += 1
        generated_units += refill_units
        refill_attempts[chunk_index] += 1
        try:
            raw_refill_verification = refill_candidate_verifier(
                refill_trajectory,
                refill_chunks,
                seed,
            )
        except Exception as error:
            raise RuntimeError("chunk refill verification failed") from error
        try:
            (
                refill_verification,
                refill_independent_local_results,
                refill_joined_output,
                refill_independent_output,
            ) = (
                _unwrap_candidate_verification(raw_refill_verification)
            )
        except TypeError as error:
            raise RuntimeError(
                "refill candidate verifier returned an invalid result"
            ) from error
        refill_verification = _validate_coverage_evidence(
            refill_verification,
            1,
            verifier_name="refill candidate verifier",
        )
        refill_verification = _bind_generation_endpoint_evidence(
            refill_verification,
            refill_generation_evidence,
            required=require_endpoint_evidence,
        )
        latest_local_results[chunk_index] = refill_verification.candidate_results[0]
        diagnostic_candidates.append(
            _VerifiedTrajectoryCandidate(
                candidate_index=next_candidate_index,
                seed=seed,
                trajectory=refill_trajectory,
                verification=refill_verification,
                independent_local_results=refill_independent_local_results,
                joined_output=refill_joined_output,
                independent_output=refill_independent_output,
                generation_evidence=refill_generation_evidence,
            )
        )
        retained = None
        if not (
            chunk_count == 1
            and refill_joined_output is not None
            and refill_verification.passed is not True
        ):
            retained = _coverage_local_candidate(
                candidate_index=next_candidate_index,
                seed=seed,
                audio=refill_trajectory[0],
                result=refill_verification.candidate_results[0],
                artifact=refill_verification.chunk_artifacts[0],
                max_local_boundary_speaker_drop=dp_boundary_limit,
            )
        if retained is not None:
            pools[chunk_index].append(retained)
        eager_result = eager_rank_one_sequence_probe()
        if eager_result is not None:
            return eager_result
        refill_preferred = (
            _preferred_release_verification(
                refill_verification,
                min_speaker_similarity=preferred_similarity,
                max_boundary_speaker_drop=preferred_boundary,
                min_squim_stoi=preferred_stoi,
                min_squim_pesq=preferred_pesq,
                min_squim_audio_duration_seconds=preferred_squim_duration,
            )
            or _preferred_release_evidence(
                refill_joined_output,
                min_speaker_similarity=preferred_similarity,
                max_boundary_speaker_drop=preferred_boundary,
                min_squim_stoi=preferred_stoi,
                min_squim_pesq=preferred_pesq,
                min_squim_audio_duration_seconds=preferred_squim_duration,
            )
        )
        refill_fast_exact = bool(
            chunk_count == 1
            and single_exact_fast_enabled
            and _single_exact_fast_release_evidence(
                refill_joined_output,
                refill_independent_output,
                min_speaker_similarity=single_exact_similarity,
                max_boundary_speaker_drop=single_exact_boundary,
                min_squim_stoi=preferred_stoi,
                min_squim_pesq=preferred_pesq,
                min_audio_duration_seconds=preferred_squim_duration,
            )
        )
        if (
            chunk_count == 1
            and preferred_single_search
            and refill_verification.passed is True
            and (refill_preferred or refill_fast_exact)
        ):
            preferred_candidate = diagnostic_candidates[-1]
            preferred_artifact = preferred_candidate.verification.chunk_artifacts[0]
            preferred_reached_hard_cap = (
                _endpoint_artifact_reached_hard_cap(preferred_artifact)
            )
            allow_natural_endpoint_waiver = bool(
                refill_preferred and preferred_reached_hard_cap
            )
            selectable = [
                candidate
                for candidate in diagnostic_candidates
                if candidate.verification.passed is True
                and math.isfinite(candidate.verification.score)
                and len(candidate.verification.chunk_artifacts) == 1
                and (
                    _preferred_release_verification(
                        candidate.verification,
                        min_speaker_similarity=preferred_similarity,
                        max_boundary_speaker_drop=preferred_boundary,
                        min_squim_stoi=preferred_stoi,
                        min_squim_pesq=preferred_pesq,
                        min_squim_audio_duration_seconds=(
                            preferred_squim_duration
                        ),
                    )
                    or _preferred_release_evidence(
                        candidate.joined_output,
                        min_speaker_similarity=preferred_similarity,
                        max_boundary_speaker_drop=preferred_boundary,
                        min_squim_stoi=preferred_stoi,
                        min_squim_pesq=preferred_pesq,
                        min_squim_audio_duration_seconds=(
                            preferred_squim_duration
                        ),
                    )
                    or (
                        single_exact_fast_enabled
                        and _single_exact_fast_release_evidence(
                            candidate.joined_output,
                            candidate.independent_output,
                            min_speaker_similarity=single_exact_similarity,
                            max_boundary_speaker_drop=single_exact_boundary,
                            min_squim_stoi=preferred_stoi,
                            min_squim_pesq=preferred_pesq,
                            min_audio_duration_seconds=(
                                preferred_squim_duration
                            ),
                        )
                    )
                    or (
                        allow_natural_endpoint_waiver
                        and _preferred_natural_endpoint_waiver(
                            candidate.verification,
                            candidate.verification.chunk_artifacts[0],
                            min_speaker_similarity=preferred_similarity,
                            max_boundary_speaker_drop=preferred_boundary,
                            min_squim_stoi=preferred_stoi,
                            min_squim_pesq=preferred_pesq,
                            min_squim_audio_duration_seconds=(
                                preferred_squim_duration
                            ),
                        )
                    )
                )
                and math.isfinite(
                    candidate_endpoint_selection_cost(
                        candidate.verification.chunk_artifacts[0]
                    )
                )
            ]
            selected_candidate = min(
                selectable,
                key=lambda candidate: (
                    candidate.verification.score
                    + candidate_endpoint_selection_cost(
                        candidate.verification.chunk_artifacts[0]
                    ),
                    candidate.candidate_index,
                ),
            )
            return CascadeResult(
                trajectory=selected_candidate.trajectory,
                verification=selected_candidate.verification,
                seed=selected_candidate.seed,
                candidate_index=selected_candidate.candidate_index,
                attempted_seeds=tuple(attempted_seeds),
                chunk_candidate_indices=(selected_candidate.candidate_index,),
                chunk_seeds=(selected_candidate.seed,),
                selection_mode="whole_trajectory",
                diagnostics=_cascade_diagnostics(diagnostic_candidates),
                generated_chunk_count=generated_chunks,
                generated_text_units=generated_units,
                chunk_candidate_counts=(len(pools[0]),),
            )
        next_candidate_index += 1

    if any(not pool for pool in pools):
        zero_coverage_transition_counts = tuple(
            sum(
                math.isfinite(
                    candidate_chunk_transition_score(
                        previous.result,
                        previous.artifact,
                        current.result,
                        current.artifact,
                    )
                )
                for previous in pools[chunk_index - 1]
                for current in pools[chunk_index]
            )
            for chunk_index in range(1, chunk_count)
        )
        sequence_search = SequenceSearchEvidence(
            eligible_candidate_counts=tuple(len(pool) for pool in pools),
            finite_transition_counts=zero_coverage_transition_counts,
            ranked_path_count=0,
        )
        raise NoQualifiedCandidateError(
            "no exact-final TTS path: at least one chunk has zero safe coverage "
            f"after {generated_chunks} generated chunks",
            diagnostics=_cascade_diagnostics(
                diagnostic_candidates,
                sequence_search,
            ),
        )

    if chunk_count > 1 and any(len(pool) > 1 for pool in pools):
        enrich_transition_artifacts_for_ranking()

    local_scores = [
        [candidate.result.score for candidate in pool]
        for pool in pools
    ]
    transitions: list[list[list[float]]] = []
    for chunk_index in range(1, chunk_count):
        transitions.append(
            [
                [
                    candidate_chunk_transition_score(
                        previous.result,
                        previous.artifact,
                        current.result,
                        current.artifact,
                    )
                    for current in pools[chunk_index]
                ]
                for previous in pools[chunk_index - 1]
            ]
        )

    # If every initial local chunk was strict, the initial exact assembly was
    # already rejected by Breeze25. Do not spend another final check
    # on that identical waveform.  Boundary-recovered initial paths are not
    # excluded because the exact whole checks have not run for them yet.
    excluded_path_list: list[tuple[int, ...]] = []
    if (
        initial_verification.passed is not True
        and initial_all_strict
        and all(pool[0].candidate_index == 0 for pool in pools)
    ):
        excluded_path_list.append((0,) * chunk_count)
    for checked_identity in sorted(checked_sequence_path_identities):
        checked_positions = tuple(
            next(
                position
                for position, candidate in enumerate(pools[chunk_index])
                if candidate.candidate_index == candidate_index
            )
            for chunk_index, candidate_index in enumerate(checked_identity)
        )
        if checked_positions not in excluded_path_list:
            excluded_path_list.append(checked_positions)
    excluded_paths = tuple(excluded_path_list)
    culprit_indices = tuple(initial_culprits)
    remaining_path_budget = path_limit - len(sequence_path_ledger)
    selections = (
        select_culprit_diverse_candidate_sequences(
            local_scores,
            transitions,
            culprit_indices=culprit_indices,
            excluded_paths=excluded_paths,
            max_paths=remaining_path_budget,
        )
        if remaining_path_budget > 0
        else ()
    )

    eligible_counts = tuple(len(pool) for pool in pools)
    finite_transition_counts = tuple(
        sum(
            math.isfinite(score)
            for row in matrix
            for score in row
        )
        for matrix in transitions
    )
    checked_paths = sequence_path_ledger
    checked = len(checked_paths)
    ranked_path_count = checked + len(selections)
    for selection in selections:
        rank = len(checked_paths) + 1
        selected = tuple(
            pools[chunk_index][position]
            for chunk_index, position in enumerate(selection.candidate_indices)
        )
        local_verification = TrajectoryGateResult(
            passed=True,
            candidate_results=tuple(candidate.result for candidate in selected),
            score=selection.total_score,
            rejection_reasons=(),
            chunk_artifacts=tuple(candidate.artifact for candidate in selected),
        )
        sequence_result = CascadeResult(
            trajectory=tuple(candidate.audio for candidate in selected),
            verification=local_verification,
            seed=None,
            candidate_index=None,
            attempted_seeds=tuple(attempted_seeds),
            chunk_candidate_indices=tuple(
                candidate.candidate_index for candidate in selected
            ),
            chunk_seeds=tuple(candidate.seed for candidate in selected),
            selection_mode="coverage_sequence_dp",
            sequence_path_rank=rank,
            diagnostics=_cascade_diagnostics(
                diagnostic_candidates,
                SequenceSearchEvidence(
                    eligible_candidate_counts=eligible_counts,
                    finite_transition_counts=finite_transition_counts,
                    ranked_path_count=ranked_path_count,
                    checked_paths=tuple(checked_paths),
                ),
            ),
            generated_chunk_count=generated_chunks,
            generated_text_units=generated_units,
            chunk_candidate_counts=tuple(len(pool) for pool in pools),
        )
        checked += 1
        try:
            final_verification = sequence_final_verifier(
                sequence_result,
                chunk_tuple,
            )
        except Exception as error:
            raise RuntimeError(
                "sequence final verification failed; refusing unverified audio"
            ) from error
        final_passed = _coverage_final_passed(final_verification)
        final_evidence = trajectory_gate_evidence(final_verification)
        checked_sequence_path_identities.add(
            sequence_result.chunk_candidate_indices
        )
        checked_paths.append(
            SequencePathEvidence(
                rank=rank,
                chunk_candidate_indices=(
                    sequence_result.chunk_candidate_indices
                ),
                chunk_seeds=sequence_result.chunk_seeds,
                final_output=final_evidence,
            )
        )
        sequence_search = SequenceSearchEvidence(
            eligible_candidate_counts=eligible_counts,
            finite_transition_counts=finite_transition_counts,
            ranked_path_count=ranked_path_count,
            checked_paths=tuple(checked_paths),
        )
        diagnostics = _cascade_diagnostics(
            diagnostic_candidates,
            sequence_search,
        )
        if final_passed:
            return replace(
                sequence_result,
                sequence_paths_checked=checked,
                diagnostics=diagnostics,
            )

    final_sequence_search = SequenceSearchEvidence(
        eligible_candidate_counts=eligible_counts,
        finite_transition_counts=finite_transition_counts,
        ranked_path_count=ranked_path_count,
        checked_paths=tuple(checked_paths),
    )
    if eager_hard_pass_result is not None:
        return replace(
            eager_hard_pass_result,
            attempted_seeds=tuple(attempted_seeds),
            sequence_paths_checked=checked,
            diagnostics=_cascade_diagnostics(
                diagnostic_candidates,
                final_sequence_search,
            ),
            generated_chunk_count=generated_chunks,
            generated_text_units=generated_units,
            chunk_candidate_counts=tuple(len(pool) for pool in pools),
        )

    raise NoQualifiedCandidateError(
        "no exact-final TTS path after "
        f"{generated_chunks} generated chunks and {checked} assembled paths",
        diagnostics=_cascade_diagnostics(
            diagnostic_candidates,
            final_sequence_search,
        ),
    )


def run_adaptive_cascade(
    chunks: Sequence[str],
    root_seed: int,
    candidate_generator: Callable[[tuple[str, ...], int], Any],
    candidate_verifier: Callable[
        [Any, tuple[str, ...], int],
        TrajectoryGateResult | CandidateVerification,
    ],
    *,
    initial_candidates: int = 1,
    max_candidates: int = 5,
    preferred_min_speaker_similarity: float = 0.25,
    preferred_max_boundary_speaker_drop: float = 0.05,
    sequence_final_verifier: (
        Callable[[CascadeResult, tuple[str, ...]], TrajectoryGateResult] | None
    ) = None,
    max_sequence_paths: int = 3,
    sequence_fallback_max_local_boundary_speaker_drop: float | None = None,
) -> CascadeResult:
    """Run the frozen deterministic fail-closed cascade through 32 candidates.

    The generator is called once per trajectory with ``root_seed + offset``.
    It receives all chunks in one call, making the shared per-trajectory seed
    contract explicit and preventing accidental per-chunk seed drift.
    """

    try:
        chunk_tuple = tuple(str(chunk) for chunk in chunks)
        base_seed = int(root_seed)
        first_stage = int(initial_candidates)
        limit = int(max_candidates)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError("invalid adaptive cascade arguments") from error
    if not chunk_tuple or any(not chunk for chunk in chunk_tuple):
        raise ValueError("adaptive cascade requires non-empty text chunks")
    if first_stage != 1:
        raise ValueError("online adaptive cascade must start with exactly one candidate")
    if limit < first_stage or limit > ADAPTIVE_CASCADE_STAGE_LIMITS[-1]:
        raise ValueError("adaptive cascade supports between 1 and 32 candidates")
    if isinstance(max_sequence_paths, (bool, np.bool_)):
        raise ValueError("max_sequence_paths must be an integer between 1 and 3")
    try:
        sequence_path_limit = operator.index(max_sequence_paths)
    except (TypeError, ValueError, OverflowError) as error:
        raise ValueError(
            "max_sequence_paths must be an integer between 1 and 3"
        ) from error
    if not 1 <= sequence_path_limit <= 3:
        raise ValueError("max_sequence_paths must be an integer between 1 and 3")
    if sequence_final_verifier is not None and not callable(sequence_final_verifier):
        raise ValueError("sequence_final_verifier must be callable")
    sequence_fallback_boundary = None
    if sequence_fallback_max_local_boundary_speaker_drop is not None:
        sequence_fallback_boundary = _finite_float(
            sequence_fallback_max_local_boundary_speaker_drop,
            minimum=0.0,
            maximum=1.0,
        )
        if sequence_fallback_boundary is None:
            raise ValueError("sequence fallback boundary threshold must be finite")
        if sequence_final_verifier is None:
            raise ValueError(
                "sequence fallback boundary relaxation requires a final verifier"
            )
    preferred_similarity = _finite_float(
        preferred_min_speaker_similarity,
        minimum=-1.0,
        maximum=1.0,
    )
    preferred_boundary = _finite_float(
        preferred_max_boundary_speaker_drop,
        minimum=0.0,
    )
    if preferred_similarity is None or preferred_boundary is None:
        raise ValueError("preferred speaker thresholds must be finite")

    attempted_seeds: list[int] = []
    candidates: list[_VerifiedTrajectoryCandidate] = []
    sequence_search_evidence: SequenceSearchEvidence | None = None

    first_seed = base_seed
    first_trajectory = candidate_generator(chunk_tuple, first_seed)
    (
        first_verification,
        first_independent_local_results,
        first_joined_output,
        first_independent_output,
    ) = _unwrap_candidate_verification(
        candidate_verifier(first_trajectory, chunk_tuple, first_seed)
    )
    attempted_seeds.append(first_seed)
    first_candidate = _VerifiedTrajectoryCandidate(
        candidate_index=0,
        seed=first_seed,
        trajectory=first_trajectory,
        verification=first_verification,
        independent_local_results=first_independent_local_results,
        joined_output=first_joined_output,
        independent_output=first_independent_output,
    )
    candidates.append(first_candidate)
    if (
        first_verification.passed
        and math.isfinite(first_verification.score)
        and (
            limit == 1
            or _preferred_speaker_verification(
                first_verification,
                min_similarity=preferred_similarity,
                max_boundary_drop=preferred_boundary,
            )
        )
    ):
        return _whole_trajectory_result(
            first_candidate,
            attempted_seeds,
            len(chunk_tuple),
            diagnostics=_cascade_diagnostics(candidates),
        )

    stages = list(
        dict.fromkeys(
            min(stage_limit, limit)
            for stage_limit in ADAPTIVE_CASCADE_STAGE_LIMITS[1:]
        )
    )
    next_candidate = first_stage
    for stage_size in stages:
        for candidate_index in range(next_candidate, stage_size):
            seed = base_seed + candidate_index
            trajectory = candidate_generator(chunk_tuple, seed)
            (
                verification,
                independent_local_results,
                joined_output,
                independent_output,
            ) = _unwrap_candidate_verification(
                candidate_verifier(trajectory, chunk_tuple, seed)
            )
            attempted_seeds.append(seed)
            candidates.append(
                _VerifiedTrajectoryCandidate(
                    candidate_index=candidate_index,
                    seed=seed,
                    trajectory=trajectory,
                    verification=verification,
                    independent_local_results=independent_local_results,
                    joined_output=joined_output,
                    independent_output=independent_output,
                )
            )
        qualified = [
            candidate
            for candidate in candidates
            if candidate.verification.passed
            and math.isfinite(candidate.verification.score)
        ]
        final_stage = stage_size == limit
        if qualified:
            preferred_qualified = [
                candidate
                for candidate in qualified
                if _preferred_speaker_verification(
                    candidate.verification,
                    min_similarity=preferred_similarity,
                    max_boundary_drop=preferred_boundary,
                )
            ]
            selectable = qualified if final_stage else preferred_qualified
            if selectable:
                selected_whole = min(
                    selectable,
                    key=lambda candidate: (
                        candidate.verification.score,
                        candidate.candidate_index,
                    ),
                )
                return _whole_trajectory_result(
                    selected_whole,
                    attempted_seeds,
                    len(chunk_tuple),
                    diagnostics=_cascade_diagnostics(candidates),
                )
            next_candidate = stage_size
            continue
        # With a final-aware callback, defer DP until all whole-trajectory
        # candidates in the request budget have been exhausted.  This keeps
        # whole trajectories globally preferred and bounds joined checks to
        # at most ``max_sequence_paths`` once per request.
        if sequence_final_verifier is not None and not final_stage:
            next_candidate = stage_size
            continue
        sequence_search = _sequence_fallback_search(
            candidates,
            attempted_seeds,
            len(chunk_tuple),
            max_paths=(sequence_path_limit if sequence_final_verifier else 1),
            max_local_boundary_speaker_drop=sequence_fallback_boundary,
        )
        sequence_results = sequence_search.results
        sequence_search_evidence = sequence_search.evidence
        if sequence_final_verifier is None:
            sequence_result = sequence_results[0] if sequence_results else None
            if sequence_result is not None and (
                final_stage
                or _preferred_speaker_verification(
                    sequence_result.verification,
                    min_similarity=preferred_similarity,
                    max_boundary_drop=preferred_boundary,
                )
            ):
                return replace(
                    sequence_result,
                    diagnostics=_cascade_diagnostics(
                        candidates,
                        sequence_search_evidence,
                    ),
                )
        else:
            checked_paths: list[SequencePathEvidence] = []
            for checked_count, sequence_result in enumerate(sequence_results, 1):
                try:
                    final_verification = sequence_final_verifier(
                        sequence_result,
                        chunk_tuple,
                    )
                except Exception as error:
                    raise RuntimeError(
                        "sequence final verification failed; refusing unverified audio"
                    ) from error
                if not isinstance(final_verification, TrajectoryGateResult):
                    raise RuntimeError(
                        "sequence final verifier returned an invalid result"
                    )
                checked_paths.append(
                    SequencePathEvidence(
                        rank=_evidence_int(sequence_result.sequence_path_rank),
                        chunk_candidate_indices=tuple(
                            _evidence_int(index)
                            for index in sequence_result.chunk_candidate_indices
                        ),
                        chunk_seeds=tuple(
                            _evidence_int(
                                seed,
                                maximum=(
                                    REQUEST_SEED_LIMIT
                                    + CASCADE_EVIDENCE_MAX_ATTEMPTS
                                ),
                            )
                            for seed in sequence_result.chunk_seeds
                        ),
                        final_output=trajectory_gate_evidence(
                            final_verification
                        ),
                    )
                )
                sequence_search_evidence = replace(
                    sequence_search.evidence,
                    checked_paths=tuple(checked_paths),
                )
                if (
                    final_verification.passed is True
                    and math.isfinite(final_verification.score)
                    and len(final_verification.candidate_results) == 1
                    and isinstance(
                        final_verification.candidate_results[0],
                        CandidateGateResult,
                    )
                    and final_verification.candidate_results[0].passed is True
                    and math.isfinite(
                        final_verification.candidate_results[0].score
                    )
                    and not final_verification.rejection_reasons
                ):
                    return CascadeResult(
                        trajectory=sequence_result.trajectory,
                        verification=sequence_result.verification,
                        seed=sequence_result.seed,
                        candidate_index=sequence_result.candidate_index,
                        attempted_seeds=sequence_result.attempted_seeds,
                        chunk_candidate_indices=(
                            sequence_result.chunk_candidate_indices
                        ),
                        chunk_seeds=sequence_result.chunk_seeds,
                        selection_mode=sequence_result.selection_mode,
                        sequence_path_rank=sequence_result.sequence_path_rank,
                        sequence_paths_checked=checked_count,
                        diagnostics=_cascade_diagnostics(
                            candidates,
                            sequence_search_evidence,
                        ),
                    )
        next_candidate = stage_size

    raise NoQualifiedCandidateError(
        f"no verified TTS trajectory after {len(attempted_seeds)} candidates",
        diagnostics=_cascade_diagnostics(
            candidates,
            sequence_search_evidence,
        ),
    )