File size: 167,063 Bytes
cfd1dd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8465007
0bfcfc6
cfd1dd4
 
 
9b45a26
1fd7e91
cfd1dd4
 
 
4aa78d2
0bfcfc6
 
cfd1dd4
 
 
675ff83
 
 
 
 
 
 
ac256e8
 
 
 
 
 
cfd1dd4
f7ccc38
 
 
 
 
 
 
 
 
 
 
 
ee844e2
 
 
 
 
8e5c220
ee844e2
686b2f2
 
 
 
 
 
 
0a513b4
 
 
 
22dbea5
 
 
 
 
 
a137721
686b2f2
 
 
 
 
 
 
 
4d8a923
 
 
 
 
 
 
 
cfd1dd4
 
 
 
594657a
 
 
 
 
 
 
 
cfd1dd4
 
8465007
 
cfd1dd4
8465007
cfd1dd4
8465007
 
 
 
 
 
 
 
 
 
 
cfd1dd4
 
 
 
 
 
 
8465007
cfd1dd4
 
 
8465007
 
 
 
 
 
 
0bfcfc6
 
 
594657a
 
 
 
 
 
0bfcfc6
cfd1dd4
 
0bfcfc6
cfd1dd4
adc5b25
cfd1dd4
 
adc5b25
cfd1dd4
 
0bfcfc6
 
 
 
 
 
594657a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bfcfc6
 
 
9b45a26
 
 
cfd1dd4
 
 
0bfcfc6
 
 
 
 
 
 
cfd1dd4
 
 
 
 
 
 
 
 
 
 
9f9e6f3
cfd1dd4
 
 
 
 
 
 
50b291b
 
ddb7303
18d1a58
 
 
 
e59b26e
 
 
 
4399565
 
 
cfd1dd4
 
 
1cc59a2
f7ccc38
 
 
 
 
1cc59a2
cfd1dd4
 
 
 
 
 
 
 
 
 
 
 
0131d9c
 
 
 
 
 
 
 
 
 
428057a
 
 
 
 
 
 
 
 
 
 
0bfcfc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96d5ffb
 
 
 
 
 
 
6a27da2
 
 
 
 
 
8698f4c
 
 
 
 
 
 
 
 
6a27da2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4399565
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96d5ffb
6a27da2
96d5ffb
 
 
 
 
6a27da2
96d5ffb
 
 
 
 
 
4399565
96d5ffb
4399565
96d5ffb
 
 
 
4399565
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96d5ffb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9282f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96d5ffb
 
 
9b45a26
 
96d5ffb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ae13a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8698f4c
 
 
 
 
 
 
 
 
 
 
 
 
4ae13a9
 
 
 
 
 
 
 
4399565
4ae13a9
4399565
4ae13a9
 
 
 
 
 
 
8698f4c
 
4ae13a9
 
 
 
 
 
 
 
 
 
 
 
 
 
0bfcfc6
 
 
 
 
0131d9c
 
 
 
 
 
 
 
428057a
 
0131d9c
 
 
 
 
 
 
 
 
428057a
 
 
 
 
 
 
 
 
 
 
 
 
0ddbedc
 
 
 
 
686b2f2
 
 
 
 
9af6005
 
 
 
 
 
 
428057a
 
 
 
 
 
 
 
ee844e2
 
 
 
 
 
 
 
 
 
 
 
1d5fa54
 
 
 
ee844e2
 
 
 
 
 
 
 
 
 
 
 
 
428057a
 
 
 
 
 
 
 
1fd7e91
 
 
18d1a58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0131d9c
 
 
 
 
 
 
 
 
 
0bfcfc6
428057a
 
 
0bfcfc6
 
 
 
 
428057a
0bfcfc6
428057a
0bfcfc6
 
 
 
9b45a26
 
 
 
 
 
 
 
0bfcfc6
9b45a26
 
 
e71a8a4
 
f7ccc38
 
 
 
 
 
 
 
 
0bfcfc6
9b45a26
 
 
 
0bfcfc6
 
 
cfd1dd4
 
 
 
 
2ee489a
 
 
 
 
 
 
 
 
cfd1dd4
 
 
 
 
50b291b
 
e59b26e
cfd1dd4
 
 
 
 
 
 
 
 
0bfcfc6
594657a
 
cfd1dd4
 
 
e59b26e
 
cfd1dd4
 
 
 
50b291b
 
e59b26e
 
4399565
cfd1dd4
8698f4c
 
 
 
cfd1dd4
 
 
675ff83
ddb7303
18d1a58
ddb7303
675ff83
 
 
ddb7303
18d1a58
 
675ff83
 
 
 
 
ddb7303
 
18d1a58
 
675ff83
 
 
cfd1dd4
0bfcfc6
594657a
cfd1dd4
 
 
594657a
cfd1dd4
f7ccc38
 
 
 
 
 
 
 
 
 
 
 
 
675ff83
 
 
 
cfd1dd4
 
 
 
 
 
 
0bfcfc6
9b45a26
cfd1dd4
 
 
 
 
 
 
9f9e6f3
cfd1dd4
 
 
1cc59a2
2f733c3
 
0131d9c
cfd1dd4
9af6005
 
 
 
 
 
 
 
 
cfd1dd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db53d73
 
 
594657a
adc5b25
594657a
 
 
 
adc5b25
594657a
 
 
db53d73
594657a
db53d73
594657a
db53d73
 
 
 
 
 
 
 
cfd1dd4
 
 
1cc59a2
ac256e8
1cc59a2
ac256e8
 
 
1cc59a2
cfd1dd4
 
 
 
 
 
 
 
50b291b
 
ddb7303
18d1a58
1cc59a2
cfd1dd4
9f9e6f3
 
cfd1dd4
 
 
 
 
 
 
2f733c3
 
0bfcfc6
2f733c3
 
 
 
0131d9c
2f733c3
 
 
1cc59a2
 
 
0bfcfc6
1cc59a2
 
 
 
 
 
 
f7ccc38
1cc59a2
 
0131d9c
0bfcfc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96d5ffb
 
d9282f5
96d5ffb
 
 
 
d9282f5
 
 
 
 
 
 
 
 
 
 
96d5ffb
 
 
 
0bfcfc6
 
75a8dd8
 
 
 
 
d9282f5
75a8dd8
 
d9282f5
 
 
 
75a8dd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9282f5
75a8dd8
 
 
 
 
d9282f5
 
0bfcfc6
 
 
 
 
8ac4623
 
 
 
0bfcfc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a27da2
0bfcfc6
 
 
6a27da2
 
 
 
 
e359478
 
 
 
6a27da2
e359478
 
6a27da2
 
 
 
 
e359478
 
6a27da2
 
 
 
e359478
6a27da2
 
e359478
 
 
 
 
 
6a27da2
 
0bfcfc6
 
 
 
2634dae
0bfcfc6
2634dae
0bfcfc6
2634dae
 
0bfcfc6
428057a
 
2634dae
 
 
 
 
 
 
 
 
0bfcfc6
2634dae
 
0131d9c
 
4ae13a9
 
 
 
 
 
 
8698f4c
 
4ae13a9
c66fe21
4ae13a9
8698f4c
4ae13a9
 
 
 
 
 
 
c66fe21
 
 
 
 
 
 
 
 
 
 
 
4ae13a9
 
 
 
 
 
 
8698f4c
4ae13a9
 
 
8698f4c
4ae13a9
 
 
 
 
 
 
 
 
 
8698f4c
 
 
 
 
4ae13a9
 
 
0131d9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bfcfc6
0131d9c
 
 
428057a
0131d9c
 
 
 
 
 
 
0bfcfc6
0131d9c
 
 
 
1fd7e91
0131d9c
 
 
 
 
 
0bfcfc6
0131d9c
 
 
 
 
 
 
 
 
 
 
 
0bfcfc6
0131d9c
 
 
 
1cc59a2
 
 
1fd7e91
 
 
 
686b2f2
 
 
 
 
 
 
 
 
 
e59b26e
 
 
 
 
 
 
 
 
 
 
428057a
686b2f2
e59b26e
428057a
 
 
e59b26e
 
 
 
 
1fd7e91
428057a
 
1fd7e91
 
 
e59b26e
686b2f2
428057a
54134cb
 
1fd7e91
 
e59b26e
 
1fd7e91
f7ccc38
428057a
1fd7e91
 
9af6005
4d8a923
 
 
 
 
 
 
9af6005
 
 
 
4d8a923
 
 
 
1fd7e91
9af6005
428057a
54134cb
 
1fd7e91
428057a
9af6005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428057a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5aba65
 
 
 
 
 
428057a
 
 
e5aba65
 
 
 
 
 
 
 
1fd7e91
e5aba65
 
 
1cd4bd6
 
 
 
e5aba65
428057a
e5aba65
428057a
54134cb
 
9af6005
 
 
 
 
428057a
 
 
 
 
9af6005
 
 
428057a
 
 
0ddbedc
9af6005
0ddbedc
428057a
 
 
 
54134cb
428057a
 
 
0bfcfc6
428057a
54134cb
 
428057a
 
0bfcfc6
428057a
 
 
 
 
 
3ae42f9
 
 
 
18d1a58
 
 
 
 
 
 
428057a
 
686b2f2
 
 
 
428057a
 
 
 
 
 
 
 
 
 
1fd7e91
 
428057a
 
 
 
18d1a58
 
 
 
 
 
 
e59b26e
 
 
 
 
428057a
 
375b364
 
428057a
375b364
 
0ddbedc
428057a
 
 
 
 
 
 
 
9af6005
 
428057a
 
 
5182876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428057a
 
 
 
 
 
 
 
64d7c9d
 
9b45a26
64d7c9d
 
0ddbedc
686b2f2
428057a
 
 
54134cb
428057a
 
 
 
 
 
 
54134cb
428057a
ee844e2
 
 
 
 
 
 
 
72933ab
9030668
 
 
 
7c9783b
 
 
 
 
ee844e2
 
 
 
 
 
 
 
 
 
 
7c9783b
 
 
 
 
 
 
 
 
 
 
9030668
 
7c9783b
96d5ffb
ee844e2
 
 
 
7c9783b
 
 
 
 
 
 
 
 
 
 
 
 
96d5ffb
9030668
 
 
 
 
96d5ffb
 
7c9783b
 
 
ee844e2
96d5ffb
 
7c9783b
 
 
9030668
7c9783b
9030668
96d5ffb
 
ee844e2
 
 
9030668
 
 
ee844e2
 
 
34dfdb4
 
 
 
 
 
 
 
 
 
 
ee844e2
 
 
 
 
 
 
 
 
 
 
 
1f0b9c7
 
 
 
 
1d5fa54
7da2e7b
ee844e2
1f0b9c7
 
 
 
 
 
 
ee844e2
34dfdb4
 
ee844e2
1d5fa54
7da2e7b
34dfdb4
 
 
 
 
 
 
 
ee844e2
 
 
 
34dfdb4
 
 
1f0b9c7
 
 
 
 
 
34dfdb4
 
 
 
 
1d5fa54
34dfdb4
ee844e2
 
 
 
 
 
 
 
 
7c9783b
ee844e2
9030668
 
ee844e2
 
 
34dfdb4
 
ee844e2
 
 
3ebb42b
 
 
ee844e2
 
7da2e7b
ee844e2
 
 
 
2c6f6f0
 
ee844e2
 
34dfdb4
9b45a26
 
ee844e2
 
 
 
 
 
 
 
 
 
 
1d5fa54
ee844e2
 
 
 
428057a
 
 
 
 
54134cb
428057a
9b45a26
428057a
54134cb
428057a
 
54134cb
428057a
e59b26e
 
 
 
 
 
428057a
 
375b364
 
0ddbedc
428057a
e59b26e
 
 
 
54134cb
 
 
0bfcfc6
9b45a26
428057a
54134cb
428057a
 
54134cb
9af6005
 
428057a
54134cb
 
cfd1dd4
 
0bfcfc6
cfd1dd4
 
 
428057a
f7ccc38
cfd1dd4
 
 
4aa78d2
ba27300
4aa78d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfd1dd4
 
 
 
 
 
 
 
 
9f9e6f3
cfd1dd4
 
9f9e6f3
 
 
cfd1dd4
cd9f12a
 
 
 
9f9e6f3
 
 
cfd1dd4
 
3ce19f0
c9db19f
1cc59a2
1fd7e91
 
9f9e6f3
1cc59a2
cfd1dd4
9f9e6f3
 
 
 
 
 
 
 
 
 
 
 
 
 
9af6005
 
50b291b
ddb7303
 
 
 
 
 
18d1a58
 
 
 
 
9f9e6f3
 
 
 
 
 
 
 
 
cfd1dd4
0131d9c
 
 
 
 
 
 
cfa8a0c
 
 
 
 
 
 
 
 
2634dae
6f2836f
 
 
2634dae
 
 
 
ef2c861
 
 
 
 
 
 
4ae13a9
 
 
 
ef2c861
 
 
 
 
 
3ae42f9
6f2836f
3ae42f9
e5aba65
 
 
 
 
0ddbedc
 
c4b52c6
0ddbedc
c4b52c6
 
de0b692
 
 
c4b52c6
de0b692
 
 
 
 
 
 
18d1a58
 
 
0ddbedc
 
 
 
5182876
0ddbedc
de0b692
5182876
de0b692
6865ce4
5b1b45e
 
6865ce4
5b1b45e
 
 
6865ce4
 
c30a632
 
6865ce4
 
 
 
0ddbedc
 
3ae42f9
 
18d1a58
55cc221
 
 
 
 
 
 
 
9a0e825
 
 
3ce19f0
720a0b8
 
 
 
 
 
 
 
 
18d1a58
720a0b8
3ce19f0
2e898ad
13b4dbd
 
 
2e898ad
13b4dbd
2e898ad
3ce19f0
 
 
 
 
55cc221
3ce19f0
27bb19f
7c9783b
 
 
 
 
 
 
 
 
3ce19f0
 
9a0e825
 
27bb19f
9030668
 
 
 
 
 
 
 
db53d73
3ebb42b
db53d73
3ebb42b
db53d73
3ebb42b
 
 
9a0e825
ee844e2
 
1d5fa54
 
 
 
ee844e2
3ae42f9
 
0ddbedc
75a8dd8
 
d9282f5
0ddbedc
6a27da2
8ac4623
 
 
6f2836f
8ac4623
 
 
1fd7e91
3ce19f0
428057a
 
 
 
 
 
3ce19f0
0131d9c
cfa8a0c
 
 
 
 
 
 
 
cfd1dd4
9f9e6f3
 
 
 
 
 
720a0b8
3ce19f0
 
 
 
720a0b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13b4dbd
ef2c861
13b4dbd
3ae42f9
13b4dbd
e5aba65
3ce19f0
13b4dbd
18d1a58
13b4dbd
7c9783b
0bfcfc6
55cc221
6f2836f
 
 
0bfcfc6
 
8ac4623
 
 
 
 
 
 
 
 
 
 
 
0bfcfc6
 
cfd1dd4
 
9f9e6f3
4ae13a9
2634dae
3ae42f9
720a0b8
55cc221
 
 
ee844e2
9030668
7c9783b
 
 
e5aba65
0131d9c
ddb7303
 
 
 
 
 
 
0131d9c
18d1a58
 
 
 
 
 
 
 
 
 
 
0bfcfc6
 
6f2836f
0bfcfc6
 
6a27da2
 
 
 
e359478
 
 
 
6a27da2
e359478
 
6a27da2
e359478
 
 
9f21143
 
6a27da2
 
 
 
e359478
6a27da2
 
 
 
 
0bfcfc6
 
6a27da2
 
 
 
 
 
 
 
0bfcfc6
8ac4623
6a27da2
0bfcfc6
 
 
 
6a27da2
 
2634dae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bfcfc6
ef2c861
 
e359478
720a0b8
 
 
18d1a58
720a0b8
 
18d1a58
720a0b8
 
 
 
 
 
18d1a58
 
 
e359478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ae42f9
 
 
 
 
0ddbedc
55cc221
 
 
7c9783b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e898ad
 
 
 
 
 
3ce19f0
 
ee844e2
7c9783b
2e898ad
 
3ce19f0
 
 
55cc221
13b4dbd
db53d73
 
 
 
 
 
 
 
55cc221
 
 
7c9783b
55cc221
96d5ffb
9030668
96d5ffb
9030668
96d5ffb
9030668
7c9783b
 
 
 
 
9030668
7c9783b
 
 
 
 
 
9030668
 
 
 
 
 
 
 
 
 
55cc221
 
 
db53d73
55cc221
9030668
 
7c9783b
 
9030668
db53d73
ee844e2
 
db53d73
 
 
ee844e2
db53d73
 
 
 
27bb19f
db53d73
9030668
 
27bb19f
db53d73
 
27bb19f
9a0e825
db53d73
1d5fa54
db53d73
 
 
 
9030668
db53d73
 
 
 
 
 
27bb19f
1d5fa54
 
 
 
 
 
 
 
7da2e7b
 
 
1d5fa54
7da2e7b
1d5fa54
9030668
 
 
 
27bb19f
 
9030668
 
 
 
 
 
55cc221
ee844e2
 
db53d73
 
 
7c9783b
ee844e2
 
7da2e7b
ee844e2
 
 
 
 
 
 
 
 
 
 
 
3ae42f9
 
a87c1b4
3ae42f9
 
 
 
1cd4bd6
18d1a58
 
 
 
 
3ae42f9
 
 
a87c1b4
3ae42f9
 
 
 
 
 
96d5ffb
 
 
 
a87c1b4
 
 
18d1a58
a87c1b4
 
 
 
 
 
 
18d1a58
 
a87c1b4
 
 
3ae42f9
96d5ffb
d9282f5
75a8dd8
 
 
 
96d5ffb
75a8dd8
 
 
 
 
 
ef2c861
 
 
 
 
e359478
 
 
 
 
 
3ae42f9
13b4dbd
3ae42f9
e359478
75a8dd8
96d5ffb
ef2c861
3ae42f9
55cc221
 
0bfcfc6
6a27da2
 
 
0bfcfc6
13b4dbd
7c9783b
0bfcfc6
13b4dbd
 
 
7c9783b
55cc221
 
 
7c9783b
55cc221
 
 
 
 
 
96d5ffb
55cc221
 
 
 
 
 
0ddbedc
6a27da2
2634dae
 
ef2c861
0ddbedc
2634dae
cfa8a0c
ef2c861
cfa8a0c
 
 
 
 
 
 
 
 
96d5ffb
55cc221
0ddbedc
 
55cc221
 
428057a
3ae42f9
 
 
 
 
e5aba65
1cd4bd6
 
 
 
e5aba65
428057a
 
3ae42f9
de0b692
 
 
 
 
 
 
428057a
 
1fd7e91
428057a
de0b692
428057a
5182876
 
 
 
 
75a8dd8
 
d9282f5
 
428057a
 
9af6005
 
 
 
 
 
375b364
 
 
 
9af6005
375b364
9af6005
375b364
c4b52c6
 
375b364
c4b52c6
375b364
 
 
ef2c861
 
 
 
 
 
 
 
 
c66fe21
 
 
 
 
 
 
 
 
ef2c861
c66fe21
 
 
ef2c861
 
 
 
 
 
 
 
 
 
 
 
 
 
4ae13a9
 
 
 
 
 
428057a
13b4dbd
720a0b8
 
0ddbedc
720a0b8
13b4dbd
 
 
 
 
 
 
 
 
428057a
0ddbedc
 
 
18d1a58
0ddbedc
 
de0b692
 
 
 
 
 
 
 
 
 
 
 
5b1b45e
18d1a58
 
 
 
 
 
 
 
5b1b45e
 
6865ce4
5b1b45e
 
 
 
 
 
18d1a58
6865ce4
5b1b45e
 
c30a632
6865ce4
 
 
0ddbedc
428057a
ddb7303
 
9f9e6f3
 
 
 
 
 
 
 
 
 
1cc59a2
cfd1dd4
 
 
1cc59a2
 
cfd1dd4
7f98965
 
 
 
 
 
 
3ce19f0
7f98965
 
 
 
 
cfd1dd4
9f9e6f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50b291b
9f9e6f3
 
 
 
 
 
 
e5aba65
9f9e6f3
18d1a58
9f9e6f3
cfd1dd4
 
4ae13a9
e5aba65
0bfcfc6
428057a
4ae13a9
e5aba65
0131d9c
e5aba65
9f9e6f3
0131d9c
9f9e6f3
 
e5aba65
720a0b8
3ae42f9
e5aba65
4ae13a9
 
7f98965
 
 
 
cfd1dd4
9af6005
 
 
 
 
 
 
 
 
375b364
 
 
cfd1dd4
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
"""Grabette fleet — operator dashboard + command broker (Docker HF Space).

A free-tier-friendly Docker Space. It is self-contained (no `grabette` import) so
it deploys standalone to HF. Responsibilities:

  * Operator UI + login via HF Spaces native OAuth (`hf_oauth: true`).
  * Command broker: an in-memory, per-owner device registry + command queue.
  * Device auth: devices call with `Authorization: Bearer <hf_token>`; we resolve
    the owner via `whoami` (cached) and group devices by HF identity.

Transport is short-polling (devices GET /api/devices/poll every couple seconds).
That polling traffic doubles as the keep-alive that stops a free Space sleeping
(sleep is timed from the last request). State is in-memory, so a restart drops
it — devices simply re-register on their next poll; durable data lives in the
device's own HF datasets, not here.

Designed to be duplicated per user: one Space = one owner = one fleet.
"""

from __future__ import annotations

import asyncio
import logging
import os
import time
import uuid
from dataclasses import dataclass, field
from datetime import datetime, timedelta, timezone
from typing import Any, Optional

from fastapi import Depends, FastAPI, Header, HTTPException, Request
from fastapi.responses import HTMLResponse, RedirectResponse

logger = logging.getLogger("grabette-fleet")
from huggingface_hub import attach_huggingface_oauth, parse_huggingface_oauth, whoami
from pydantic import BaseModel

# A device is "online" if the fleet heard from it within this window. Liveness
# no longer rides on the command poll (long-polling holds that open ~LONG_POLL_S,
# and muxing runs off the poll loop) but on a dedicated lightweight HEARTBEAT the
# device sends every DEVICE_HEARTBEAT_S. So this only needs to cover a missed
# heartbeat + jitter (~3× the interval) — small, for fast disconnect detection.
DEVICE_HEARTBEAT_S = 5.0   # must match the device's relay heartbeat cadence
ONLINE_WINDOW = 15.0       # ~3 missed heartbeats → offline within ~15s
# Only discard a device's queued commands once it's been gone THIS long — far
# beyond ONLINE_WINDOW and any transient blip. Clearing on the mere online
# window (15s) would nuke an in-flight start/stop the instant a device flapped,
# breaking group sync; a device that's briefly "offline" but still holding a
# long-poll receives its queued command the moment it's enqueued regardless.
STALE_QUEUE_S = 120.0
WHOAMI_TTL = 300.0
# Long-polling: the poll endpoint holds the connection open up to this many
# seconds when the device's queue is empty, returning the instant a command is
# enqueued (see _enqueue). This cuts command-delivery latency from ~1 poll
# interval to a network round-trip — notably a fanned-out group STOP reaches a
# peer in ~ms, so grouped episodes end within ~ms of each other instead of ~1s.
# MUST stay below BOTH the HF Space reverse-proxy idle timeout AND ONLINE_WINDOW
# (a held poll only refreshes last_seen when it returns, so a device mid-hold
# must not age out as offline). Set to 0 to DISABLE → classic short-polling: the
# poll returns immediately and the client throttles to its poll_interval. The
# relay client auto-detects which mode the server is in, so flipping this needs
# no device change — the escape hatch if the Space's proxy/sleep misbehaves.
LONG_POLL_S = 25.0
# LeRobot dataset generation: the fleet gathers the selected tasks' episodes,
# has each device push its OWN streams to a shared raw dataset (by role), then
# triggers a processing Space that converts raw → LeRobot (mono or bimanual,
# per the device set). Overridable so the test Space can be targeted.
LEROBOT_SPACE_URL = os.environ.get(
    "GRABETTE_y.LEROBOT_SPACE_URL", "https://pollen-robotics-grabette-slam-duplicate.hf.space"
).rstrip("/")
# Lead time before a group's synchronized start actually fires. The device
# warms its hardware DURING this lead, then waits out the shared T0 on its own
# NTP-disciplined clock. The lead must cover (poll delivery + hardware warmup)
# so warmup finishes BEFORE T0 — otherwise the variable warmup leaks into the
# start and the devices drift apart. Warmup is only long when the OAK-D is cold
# (multi-second cold boot); once it has recorded recently it stays warm (device
# keepalive), so we use two leads:
# Must cover (poll delivery to the peer ~2.5s + OAK-D cold boot). Measured: a
# peer overran a 12s lead by ~0.7s (cold boot ≈10.2s, but it only had ~9.5s
# after poll delivery), so 15s gives ~2.3s margin over the observed cold boot.
GROUP_START_LEAD_COLD_S = 15.0   # OAK-D likely asleep → cover poll delivery + cold boot
# Even when warm, the lead must exceed the WORST-CASE command delivery to a
# peer: a device polls only every ~2.5s AND its relay is blocked while it muxes
# the previous episode's stop (several seconds), so a too-short warm lead makes
# the peer receive its start_capture after T0 (→ started late, best-effort — it
# no longer drops the episode, but it's desynced). 3s covers the common case;
# the real cure for back-to-back on-time starts is non-blocking relay muxing.
GROUP_START_LEAD_WARM_S = 1.0
# Fleet can't see the OAK-D power state directly; it infers "warm" from the time
# since the session's last recording stop. This window MUST stay safely below
# the device's OAK-D keepalive (GRABETTE oakd_keepalive_s, default 30s) so that
# whenever fleet says "warm" the OAK-D is DEFINITELY still powered — a false
# "warm" would desync the start, a false "cold" only costs an unnecessarily long
# lead. Fleet's stop timestamp is the dispatch time, earlier than the device's
# actual keepalive countdown start, which makes the estimate extra conservative.
OAK_WARM_WINDOW_S = 25.0
# Lead for a lead-based SYNCHRONIZED stop (shared future T_stop fanned to every
# member so they end together). CURRENTLY UNUSED: stops are dispatched
# immediately (see _dispatch_episode_stop) so a button press feels instant and
# peers trail only by the ~1s poll delivery. Kept here (with the device-side
# CaptureScheduler.schedule_stop path) so lead-based sync can be re-enabled
# without re-plumbing — e.g. if delivery latency ever grew. To switch back:
# have _dispatch_episode_stop send args={"stop_at_utc": now + GROUP_STOP_LEAD_S}.
GROUP_STOP_LEAD_S = 3.0


# --- device identity (Bearer token -> HF username, cached) -------------------
_whoami_cache: dict[str, tuple[str, float]] = {}
# Derived namespace list per owner (username + orgs) for the dataset owner
# dropdown — NOT the raw token. Device tokens list orgs reliably (unlike the
# short-lived operator OAuth token), so we compute the list from a device token
# when one passes through, cache the RESULT, and discard the token. In a shared
# Space this avoids holding every user's write-capable token in memory.
# owner -> (expiry_epoch, [namespaces]).
_namespaces_cache: dict[str, tuple[float, list[str]]] = {}
_NAMESPACES_TTL = 600.0


def _cached_user(token: str) -> Optional[str]:
    """Fast path: no I/O. Returns the cached username if still fresh."""
    hit = _whoami_cache.get(token)
    if hit and hit[1] > time.time():
        return hit[0]
    return None


def _whoami_blocking(token: str) -> str:
    """Slow path: hits the HF API. Synchronous — the huggingface_hub client
    has no async variant, so this must always be run off the event loop
    thread (see verify_user) or a single cache-miss stalls every other
    request the whole broker is serving (all owners' polling, dispatch, and
    the synchronized-start scheduling), since this process runs a single
    worker with all state in memory.
    """
    try:
        info = whoami(token=token)
    except Exception as e:  # noqa: BLE001
        raise ValueError(f"Invalid HF token: {e}") from e
    name = (info or {}).get("name", "")
    if not name:
        raise ValueError("Could not resolve HF username")
    _whoami_cache[token] = (name, time.time() + WHOAMI_TTL)
    return name


async def verify_user(token: str) -> str:
    cached = _cached_user(token)
    if cached is not None:
        return cached
    return await asyncio.to_thread(_whoami_blocking, token)


async def device_auth(authorization: Optional[str] = Header(None)) -> tuple[str, str]:
    """FastAPI dep: resolve (owner, token) from a device's Bearer HF token.

    Returns the raw token too — handlers use it transiently within the request
    (e.g. dispatching a device's own Space call). It is deliberately NOT retained
    anywhere on the fleet: in a shared Space that would concentrate every user's
    write-capable HF token in one process. Owner identity is resolved via a cached
    whoami; the operator's namespace list is derived + cached separately (see
    _touch_namespaces) so no raw token needs to be kept.
    """
    if not authorization or not authorization.startswith("Bearer "):
        raise HTTPException(401, "Missing 'Authorization: Bearer <hf_token>'")
    token = authorization[len("Bearer ") :].strip()
    try:
        owner = await verify_user(token)
    except ValueError as e:
        raise HTTPException(401, str(e)) from e
    return owner, token


async def device_owner(authorization: Optional[str] = Header(None)) -> str:
    """FastAPI dep: resolve just the device's owner from its Bearer HF token."""
    owner, _token = await device_auth(authorization)
    return owner


def _cache_namespaces_blocking(owner: str, token: str) -> None:
    """Blocking: resolve the owner's pushable namespaces (username + orgs) from a
    device token and cache the RESULT — never the token itself."""
    names = [owner]
    try:
        info = whoami(token=token)
        names += [o["name"] for o in (info.get("orgs") or []) if o.get("name")]
    except Exception:
        logger.debug("namespaces whoami failed for %s", owner, exc_info=True)
    _namespaces_cache[owner] = (time.time() + _NAMESPACES_TTL, list(dict.fromkeys(names)))


def _touch_namespaces(owner: str, token: Optional[str]) -> None:
    """TTL-gated, fire-and-forget: refresh the owner's namespace list from a
    passing device token, so the dataset-owner dropdown works WITHOUT the fleet
    keeping the token. Marks the cache fresh up front to avoid a refresh stampede."""
    if not token:
        return
    ent = _namespaces_cache.get(owner)
    if ent and ent[0] > time.time():
        return  # still fresh
    _namespaces_cache[owner] = (time.time() + _NAMESPACES_TTL, ent[1] if ent else [owner])
    t = asyncio.create_task(asyncio.to_thread(_cache_namespaces_blocking, owner, token))
    _bg_tasks.add(t)
    t.add_done_callback(_bg_tasks.discard)


def operator_auth(request: Request) -> tuple[str, Optional[str]]:
    """Resolve (owner, oauth_access_token) for the logged-in operator, or 401.

    The OAuth token identifies the operator (their namespace). The fleet keeps
    no durable state of its own — the task/episode view is aggregated live from
    connected devices' reports (see _reported_tasks)."""
    info = parse_huggingface_oauth(request)
    if info is None:
        raise HTTPException(401, "Not logged in")
    owner = info.user_info.preferred_username or info.user_info.name
    return owner, getattr(info, "access_token", None)


def operator_name(request: Request) -> str:
    """Resolve the logged-in operator's HF username, or 401."""
    return operator_auth(request)[0]


# --- in-memory fleet state ---------------------------------------------------
@dataclass
class Command:
    id: str
    type: str
    args: dict[str, Any]
    status: str = "pending"  # pending | sent | done
    result: Optional[dict[str, Any]] = None
    created_at: float = field(default_factory=time.time)
    done_at: Optional[float] = None


@dataclass
class Device:
    device_id: str
    name: str
    capabilities: list[str]
    hand: str = ""  # "left" or "right", reported by the device on register
    ip: str = ""  # device LAN IPv4, reported by the device on register
    battery: Optional[float] = None  # last-reported battery %, sent via heartbeat
    # Device self-reported activity, sent on the heartbeat:
    # "" (device not updated / unknown) | idle | capturing | uploading | processing.
    # Empty falls back to fleet-side inference (see _device_activity).
    reported_status: str = ""
    # This device's recorded tasks, reported on register (see TaskManager.report_tasks).
    # The device is the durable source of truth; the fleet aggregates these across
    # connected devices (phase 2). Stored but not yet consumed in phase 1.
    tasks: list[dict] = field(default_factory=list)
    # Bumped each register (when tasks are (re)reported). Feeds the aggregation
    # cache signature so it recomputes only when a device's report changed.
    report_rev: int = 0
    last_seen: float = field(default_factory=time.time)
    queue: list[Command] = field(default_factory=list)
    history: list[Command] = field(default_factory=list)
    pending_delete: bool = False
    # Set whenever a command is enqueued (see _enqueue) to release a long-poll
    # holding on this device. Excluded from repr/eq — it's live runtime state,
    # never serialized (devices aren't persisted). asyncio.Event() binds to a
    # loop lazily (on first await), so constructing it off-loop here is fine.
    wakeup: asyncio.Event = field(default_factory=asyncio.Event, repr=False, compare=False)

    @property
    def online(self) -> bool:
        return (time.time() - self.last_seen) < ONLINE_WINDOW


FLEET: dict[str, dict[str, Device]] = {}  # owner -> device_id -> Device


def _fleet_of(owner: str) -> dict[str, Device]:
    return FLEET.setdefault(owner, {})


def kind_of(dev: Device) -> str:
    """Mirrors the frontend's kindOf(): classify a device as grabette/gripette/casquette."""
    s = f"{dev.name} {dev.device_id} {' '.join(dev.capabilities)}".lower()
    if "gripette" in s:
        return "gripette"
    if "casquette" in s:
        return "casquette"
    return "grabette"


def _device_slot(dev: Device) -> Optional[str]:
    """The task-signature role this device fills: 'left'/'right' for a handed
    grabette, 'casquette' for a casquette, else None (not recordable)."""
    k = kind_of(dev)
    if k == "casquette":
        return "casquette"
    if k == "grabette" and dev.hand in ("left", "right"):
        return dev.hand
    return None


# --- tasks (registry, source of truth for task names sent to devices) --------
# A task is a *type of action*. It carries only the device *roles* it expects
# (its "signature"), never concrete device ids — those live on the group. The
# task NAME is the stable join key devices resolve locally via
# get_or_create_task, so it must come from this single registry.
VALID_SLOTS = ("left", "right", "casquette")


@dataclass
class Task:
    id: str
    name: str
    description: str = ""
    # Expected device roles, a subset of VALID_SLOTS (e.g. ["left","right"] for
    # a bimanual task, ["right"] for a single right-hand one). Empty = no
    # constraint. Used to validate the group assigned to the task and to tell
    # the dataset builder which roles produce data.
    device_signature: list[str] = field(default_factory=list)
    created_at: float = field(default_factory=time.time)


TASKS: dict[str, dict[str, Task]] = {}  # owner -> task_id -> Task


def _tasks_of(owner: str) -> dict[str, Task]:
    return TASKS.setdefault(owner, {})


# --- task/episode aggregation from device reports ---------------------------
# The devices are the durable source of truth for tasks and for who recorded each
# episode. On connect each device reports its tasks (see TaskManager.report_tasks);
# the fleet merges the reports from currently-connected devices to reconstruct the
# task/episode view — so ANY operator sees the existing tasks and can generate a
# dataset from them, regardless of which HF account did the original acquisition.

# Task names to hide briefly after an edit(rename)/delete, until the devices have
# processed the command and re-reported without them. Bridges the window where a
# stale report would otherwise resurrect a just-deleted/renamed task. owner ->
# {name: expiry_epoch}.
TASK_SUPPRESSED: dict[str, dict[str, float]] = {}
_SUPPRESS_S = 20.0
# Grace after a session stops before its episodes can be considered "orphaned":
# covers the window where one member has registered + re-reported the episode but
# the peer hasn't yet, so an unfinished recording isn't mistaken for a lost pair.
ORPHAN_GRACE_S = 30.0
# Orphaned-episode reconciliation is EVENT-DRIVEN: recomputed when a device
# registers (connects / re-reports its tasks) — the only moments orphans change —
# and cached here, so the operator UI just reads the cache instead of forcing a
# recompute on every poll. owner -> list of orphan-episode dicts.
ORPHANS_PENDING: dict[str, list[dict]] = {}


def _suppress_task_name(owner: str, name: str) -> None:
    TASK_SUPPRESSED.setdefault(owner, {})[name] = time.time() + _SUPPRESS_S


def _suppressed_names(owner: str) -> set[str]:
    now = time.time()
    supp = TASK_SUPPRESSED.get(owner)
    if not supp:
        return set()
    # Prune expired so a name can come back if it's ever legitimately re-created.
    for n in [n for n, exp in supp.items() if exp <= now]:
        del supp[n]
    return set(supp)


def _devices_reporting_task(owner: str, name: str) -> list["Device"]:
    """Online devices whose latest report includes a task with this name."""
    out = []
    for dev in _fleet_of(owner).values():
        if dev.online and any((t.get("name") == name) for t in (dev.tasks or [])):
            out.append(dev)
    return out

def _task_episode_items(t: dict):
    """Yield (episode_id, members) for a reported task, supporting the compact
    'groups' format (episodes grouped by shared membership) and the legacy
    per-episode 'episodes' format (older device firmware)."""
    if "groups" in t:
        for grp in t.get("groups", []):
            members = grp.get("members") or {}
            for eid in grp.get("episode_ids", []):
                if eid:
                    yield eid, members
    else:
        for ep in t.get("episodes", []):
            eid = ep.get("episode_id")
            if eid:
                yield eid, (ep.get("members") or {})


# Aggregation is memoized per owner: recomputing it (O(all reported episodes))
# on every dashboard poll doesn't scale. A cheap signature over the owner's
# devices (online status + a per-device report revision + suppressed names)
# tells us when anything that feeds the aggregation actually changed; otherwise
# we return the cached result. owner -> (signature, aggregation).
_REPORTED_CACHE: dict[str, tuple] = {}


def _reported_sig(owner: str):
    devs = _fleet_of(owner)
    return (tuple(sorted((d.device_id, d.online, d.report_rev) for d in devs.values())),
            tuple(sorted(_suppressed_names(owner))))


def _compute_reported_tasks(owner: str) -> dict[str, dict]:
    agg: dict[str, dict] = {}
    suppressed = _suppressed_names(owner)  # just-edited/deleted names, hidden briefly
    for dev in _fleet_of(owner).values():
        if not dev.online:
            continue
        for t in dev.tasks or []:
            name = t.get("name")
            if not name or name in suppressed:
                continue
            entry = agg.setdefault(name, {"description": "", "device_signature": [], "episodes": {}})
            if t.get("description") and not entry["description"]:
                entry["description"] = t["description"]
            if t.get("device_signature") and not entry["device_signature"]:
                entry["device_signature"] = list(t["device_signature"])
            for eid, ep_members in _task_episode_items(t):
                members = entry["episodes"].setdefault(eid, {})
                for role, who in ep_members.items():
                    members.setdefault(role, who)
    return agg


def _reported_tasks(owner: str) -> dict[str, dict]:
    """Merge online devices' reported tasks, keyed by task name (memoized). The
    SAME episode is reported by every member device (same episode_id + members),
    so we dedup by episode_id and union members. Returns {name: {"description",
    "device_signature", "episodes": {episode_id: {role: {device_id, name}}}}}.
    The result is cached and must be treated as READ-ONLY by callers."""
    sig = _reported_sig(owner)
    cached = _REPORTED_CACHE.get(owner)
    if cached is not None and cached[0] == sig:
        return cached[1]
    agg = _compute_reported_tasks(owner)
    _REPORTED_CACHE[owner] = (sig, agg)
    return agg


def _episode_started_at(episode_id: str) -> float:
    """Best-effort epoch seconds from an episode id (UTC 'YYYYmmdd_HHMMSS')."""
    try:
        return datetime.strptime(episode_id, "%Y%m%d_%H%M%S").replace(tzinfo=timezone.utc).timestamp()
    except ValueError:
        return 0.0


def _episodes_from_entry(entry: Optional[dict]) -> list[dict]:
    """Normalise a _reported_tasks entry's episodes into a sorted list carrying
    both members (role → {device_id, name}) and a plain roles map (role → id)."""
    if not entry:
        return []
    out = []
    for eid, members in entry["episodes"].items():
        out.append({
            "episode_id": eid,
            "members": members,
            "roles": {role: who.get("device_id") for role, who in members.items()},
            "started_at": _episode_started_at(eid),
        })
    out.sort(key=lambda e: e["episode_id"])
    return out


def _online_episode_reporters(owner: str) -> dict[str, dict]:
    """From CONNECTED devices, map episode_id → {role: {device_id, name}} built
    from each reporting device's slot. Lets the fleet reconstruct who recorded an
    episode saved before the device persisted its members — the reporting device
    IS a member, and its role is its slot. One pass over the reports."""
    out: dict[str, dict] = {}
    for dev in _fleet_of(owner).values():
        if not dev.online:
            continue
        role = _device_slot(dev)
        if not role:
            continue
        who = {"device_id": dev.device_id, "name": dev.name}
        for t in dev.tasks or []:
            for eid, _members in _task_episode_items(t):
                if eid:
                    out.setdefault(eid, {}).setdefault(role, who)
    return out


def _reconcile_tasks(owner: str) -> None:
    """Ensure a Task exists locally for every task reported by connected devices,
    so a freshly-connected operator's registry is populated from the devices (the
    source of truth). Purely in-memory: the fleet persists nothing, so this is
    rebuilt from the reports on every restart."""
    tasks = _tasks_of(owner)
    by_name = {t.name: t for t in tasks.values()}
    for name, entry in _reported_tasks(owner).items():
        t = by_name.get(name)
        if t is None:
            tid = uuid.uuid4().hex[:8]
            tasks[tid] = Task(id=tid, name=name, description=entry["description"],
                              device_signature=list(entry["device_signature"]))
        else:
            # Backfill from the authoritative device report where we're missing it.
            if not t.device_signature and entry["device_signature"]:
                t.device_signature = list(entry["device_signature"])
            if not t.description and entry["description"]:
                t.description = entry["description"]


def _orphan_episodes(owner: str) -> list[dict]:
    """Episodes that lost a pair — detected purely from the live reports, no
    tombstone. An episode is orphaned when one of its member devices is ONLINE
    but no longer reports it (it deleted the episode / task), while another online
    member still holds it. This is what surfaces after a multi-device task was
    deleted with a peer offline: on reconnect, the peer's copies are seen as
    orphaned (their now-online partner deleted them) and cleanup is proposed.

    Episodes whose missing member is OFFLINE are NOT flagged — we can't tell if
    that device still has them. Just-deleted (suppressed) task names are skipped
    so a normal both-online delete doesn't flash transient orphans mid-teardown.
    Returns one entry per orphaned episode."""
    fleet = _fleet_of(owner)
    suppressed = _suppressed_names(owner)
    online = {d.device_id: d for d in fleet.values() if d.online}
    # Episodes still "in flux" — being recorded now, or just stopped — must NOT be
    # flagged: one member registers + re-reports before the other, so for a few
    # seconds it exists on one device and not (yet) the peer, which looks orphaned
    # but is just an unfinished recording. Skip any episode in an open session, or
    # in a session stopped within the grace window (covers the re-report lag).
    now = time.time()
    in_flux: set[str] = set()
    for s in _sessions_of(owner).values():
        if s.status == "open" or (s.last_stop_at is not None and now - s.last_stop_at < ORPHAN_GRACE_S):
            for ep in s.episodes:
                eid = ep.get("episode_id")
                if eid:
                    in_flux.add(eid)
    dev_eps: dict[str, set[str]] = {}          # device_id -> episode ids it reports
    ep_info: dict[str, dict] = {}              # episode_id -> {members, task}
    for dev in online.values():
        eids: set[str] = set()
        for t in dev.tasks or []:
            name = t.get("name")
            if not name or name in suppressed:
                continue
            for eid, ep_members in _task_episode_items(t):
                eids.add(eid)
                ep_info.setdefault(eid, {"members": ep_members, "task": name})
        dev_eps[dev.device_id] = eids

    def _name(mid: str, fallback: str) -> str:
        return online[mid].name if mid in online else (fallback or mid)

    out = []
    for eid, info in ep_info.items():
        if eid in in_flux:
            continue  # being recorded / just stopped — not a real orphan
        member_ids = {w.get("device_id"): w.get("name") for w in info["members"].values() if w.get("device_id")}
        deleters = [(m, _name(m, n)) for m, n in member_ids.items()
                    if m in online and eid not in dev_eps.get(m, set())]
        holders = [(m, _name(m, n)) for m, n in member_ids.items()
                   if m in online and eid in dev_eps.get(m, set())]
        if deleters and holders:  # a peer deleted it, but someone online still has it
            out.append({
                "episode_id": eid, "task": info["task"], "started_at": _episode_started_at(eid),
                "holders": [{"device_id": m, "name": n} for m, n in holders],
                "deleted_by": [{"device_id": m, "name": n} for m, n in deleters],
            })
    return out


def _task_name(owner: str, task_id: str) -> str:
    t = _tasks_of(owner).get(task_id)
    return t.name if t else ""


@dataclass
class Group:
    id: str
    name: str
    left: str = ""  # device_id of the left-hand grabette in this group
    right: str = ""  # device_id of the right-hand grabette in this group
    casquette: str = ""  # device_id of the casquette in this group
    created_at: float = field(default_factory=time.time)
    # (No task here — the task is chosen when a session is launched, not stored
    # on the group. See Session.)


GROUPS: dict[str, dict[str, Group]] = {}  # owner -> group_id -> Group


def _groups_of(owner: str) -> dict[str, Group]:
    return GROUPS.setdefault(owner, {})


# --- sessions (fleet-only: the recording-run + upload manifest) --------------
# A session is a run of one or more episodes recorded together for ONE task.
# It carries its own role→device membership (a "group of one" is just a
# single-entry map — no Group object is materialised) and the per-episode
# manifest: which physical device held which role for each episode. That
# manifest is what lets the dataset builder regroup an episode's data even
# when the device filling a role changes between episodes.
@dataclass
class Session:
    id: str
    task_id: str
    members: dict[str, str]  # role -> device_id, captured at launch
    status: str = "open"     # open | closed
    # Optimistic recording indicator: True between an episode start and its
    # stop. Maintained on every start/stop path (operator + physical button).
    # Not a hardware truth (real per-device state is reconciled in phase 3) —
    # enough to drive the "● recording" indicator in the UI.
    recording: bool = False
    # Epoch time of the last episode stop dispatched for this session. Drives
    # the warm/cold lead decision: within OAK_WARM_WINDOW_S the OAK-D is still
    # powered (device keepalive), so the next episode can use the short lead.
    # None until the first episode stops → first episode uses the cold lead.
    last_stop_at: Optional[float] = None
    # "stopping" phase, reconciled against reality: set True when an episode stop
    # is dispatched, and cleared when every dispatched stop_capture command has
    # reported its result (i.e. the devices actually finished tearing down + mux)
    # — so the UI's "stopping" indicator ends when the devices really stop, not
    # after a fixed guess. pending_stop_cmds holds the command ids still awaited.
    stopping: bool = False
    pending_stop_cmds: set[str] = field(default_factory=set)
    # Each entry: {"episode_id": str, "roles": {role: device_id}, "started_at": float}
    episodes: list[dict[str, Any]] = field(default_factory=list)
    started_at: float = field(default_factory=time.time)


SESSIONS: dict[str, dict[str, Session]] = {}  # owner -> session_id -> Session


@dataclass
class DatasetJob:
    """A LeRobot dataset build. Transient runtime state (not persisted): the
    fleet dispatches per-device uploads, waits for them, then (step 3) triggers
    the processing Space and tracks it to completion."""
    id: str
    task_ids: list[str]
    roles: list[str]              # the shared device signature of the selected tasks
    raw_repo: str                 # {owner}/grabette-raw-<job> — deleted after processing
    target_repo: str              # {owner}/<name> — the resulting LeRobot dataset
    status: str = "uploading"     # uploading | raw_ready | processing | done | error
    message: str = ""
    # 0..1 fraction for the determinate upload phase; None once processing starts
    # (the Space conversion runs opaquely behind one blocking device call, so the
    # UI shows an indeterminate bar there). 1.0 when done.
    progress: Optional[float] = 0.0
    result_url: Optional[str] = None
    error: Optional[str] = None
    upload_cmds: dict[str, str] = field(default_factory=dict)  # device_id -> command id
    created_at: float = field(default_factory=time.time)


DATASET_JOBS: dict[str, dict[str, DatasetJob]] = {}  # owner -> job_id -> job


def _dataset_jobs_of(owner: str) -> dict[str, DatasetJob]:
    return DATASET_JOBS.setdefault(owner, {})


def _sessions_of(owner: str) -> dict[str, Session]:
    return SESSIONS.setdefault(owner, {})


def _open_session_for_device(owner: str, device_id: str) -> Optional[Session]:
    for s in _sessions_of(owner).values():
        if s.status == "open" and device_id in s.members.values():
            return s
    return None


# --- device activity: what a device is doing, for the operator UI + the recording
# gates. Prefer what the fleet can INFER on its own from the work it dispatched (an
# in-flight upload/process command in the device's queue); then the device's own
# self-reported status (which also covers dashboard-initiated work the fleet never
# sees); then capture inferred from open-session membership. Pure in-memory reads,
# no extra I/O, so it stays cheap at fleet scale.
_RECORDING_BLOCKERS = ("uploading", "processing")  # dataset work that must not overlap a recording


def _device_activity(owner: str, dev: Device) -> str:
    """One of: idle | capturing | uploading | processing."""
    # Dataset work the fleet ITSELF dispatched — authoritative, and the device may
    # not self-report it (relay commands don't create a local job). A command is
    # removed from the queue on result, so anything left here is live.
    types = {c.type for c in dev.queue}
    if "process_dataset" in types:
        return "processing"
    if "upload_episodes" in types:
        return "uploading"
    # Device self-reported activity — covers local dashboard work (SLAM push /
    # episode upload) and live capture that the fleet can't see on its own.
    st = (dev.reported_status or "").strip().lower()
    if st in ("capturing", "uploading", "processing"):
        return st
    # Capture inferred from open-session membership (fallback if not reported).
    if _open_session_for_device(owner, dev.device_id) is not None:
        return "capturing"
    return "idle"


def _busy_recording_blockers(owner: str, device_ids) -> list[str]:
    """Subset of device_ids currently tied up by dataset work (upload/convert),
    which must be free before they can (re)start a recording."""
    fleet = _fleet_of(owner)
    return [did for did in device_ids
            if (d := fleet.get(did)) is not None
            and _device_activity(owner, d) in _RECORDING_BLOCKERS]


def _remove_from_groups(owner: str, device_id: str) -> None:
    for g in _groups_of(owner).values():
        if g.left == device_id:
            g.left = ""
        if g.right == device_id:
            g.right = ""
        if g.casquette == device_id:
            g.casquette = ""


def _validate_task_signature(owner: str, task_id: str, slots: set[str]) -> None:
    """The devices launched for a task must fill exactly the task's required
    roles — exact match (not just superset) so the dataset builder knows
    precisely which roles produce data. No-op when the task has no signature."""
    t = _tasks_of(owner).get(task_id)
    if t is None:
        raise HTTPException(404, f"Task {task_id} not found")
    if t.device_signature and set(t.device_signature) != slots:
        raise HTTPException(400, {
            "message": "selected devices don't match the task's required devices",
            "task": t.name,
            "required": sorted(t.device_signature),
            "got": sorted(slots),
        })


# --- persistence: none -------------------------------------------------------
# The fleet keeps NO durable state of its own. Devices are the source of truth:
# each re-registers on connect and reports its tasks + per-episode membership
# (see TaskManager.report_tasks), which the fleet aggregates in memory (see
# _reported_tasks / _reconcile_tasks). This means ANY operator sees the existing
# tasks and can build a dataset from them, regardless of which HF account did the
# acquisition — and nothing is ever written to an HF namespace on the fleet's
# behalf. Groups and open sessions are runtime-only and reset on a Space restart.

# Strong refs to fire-and-forget background tasks (e.g. dataset jobs). asyncio
# only weakly references tasks, so without this the GC can cancel one mid-run.
_bg_tasks: set[asyncio.Task] = set()


def _enqueue(dev: "Device", cmd: Command) -> None:
    """Queue a command for a device AND wake any long-poll holding on it, so the
    command is delivered on the next network round-trip instead of the next poll
    interval. Centralized on purpose: a bare dev.queue.append() would silently
    strand the command for up to LONG_POLL_S until the hold times out."""
    dev.queue.append(cmd)
    dev.wakeup.set()


async def _operator_loaded(request: Request) -> str:
    """Resolve the authenticated operator (namespace). Kept as the single
    dependency every operator-facing handler uses; there is no snapshot to load
    anymore — the task/episode view is aggregated live from device reports."""
    owner, _token = operator_auth(request)
    return owner


# --- app + OAuth -------------------------------------------------------------
app = FastAPI(title="Grabette fleet")
attach_huggingface_oauth(app)  # adds /oauth/huggingface/{login,logout,callback}


@app.get("/healthz")
async def healthz() -> dict[str, str]:
    """Public, auth-free liveness ping. A device hits this to WAKE the (free-tier,
    sleep-when-idle) Space and confirm it's up before starting OAuth — so the
    OAuth callback, which HF routes through this Space, lands on a running relay
    even when no operator has a fleet dashboard tab open to keep it warm."""
    return {"status": "ok"}


# === device-facing API (Bearer auth) ========================================
class RegisterReq(BaseModel):
    device_id: str
    name: str = ""
    capabilities: list[str] = []
    hand: str = ""  # "left" or "right" (from GRABETTE_HAND on the device)
    ip: str = ""  # device LAN IPv4, best-effort
    tasks: list[dict] = []  # this device's recorded tasks (source of truth for aggregation)


class ResultReq(BaseModel):
    device_id: str
    command_id: str
    result: dict[str, Any] = {}


@app.post("/api/devices/register")
async def register(req: RegisterReq, auth: tuple[str, str] = Depends(device_auth)) -> dict[str, Any]:
    owner, token = auth
    _touch_namespaces(owner, token)  # refresh the owner's org list (token not kept)
    fleet = _fleet_of(owner)
    dev = fleet.get(req.device_id)
    if dev is None:
        dev = Device(req.device_id, req.name or req.device_id, req.capabilities,
                     hand=req.hand, ip=req.ip, tasks=req.tasks)
        fleet[req.device_id] = dev
    else:
        dev.name = req.name or dev.name
        dev.capabilities = req.capabilities or dev.capabilities
        dev.hand = req.hand or dev.hand
        dev.ip = req.ip or dev.ip
        # Reported every register: the device is authoritative, so replace wholesale.
        dev.tasks = req.tasks
    dev.report_rev += 1  # invalidates the aggregation cache for this owner
    dev.last_seen = time.time()
    # A device just (re)reported its tasks — the moment orphaned episodes can
    # appear/change. Recompute the reconciliation snapshot now (event-driven)
    # rather than on every operator poll.
    ORPHANS_PENDING[owner] = _orphan_episodes(owner)
    return {"status": "ok", "pending": len(dev.queue)}


@app.post("/api/devices/heartbeat")
async def heartbeat(device_id: str, battery: Optional[float] = None,
                    status: Optional[str] = None,
                    auth: tuple[str, str] = Depends(device_auth)) -> dict[str, str]:
    """Lightweight liveness ping, sent every DEVICE_HEARTBEAT_S independently of
    the (long-held) command poll — this is what keeps last_seen fresh and lets
    the fleet detect a disconnect within ONLINE_WINDOW. Deliberately does no HF
    I/O so it stays cheap at a few-second cadence. Also carries the device's
    battery % and its self-reported activity, so the fleet can show device state
    without polling."""
    owner, _token = auth
    dev = _fleet_of(owner).get(device_id)
    if dev is None:
        raise HTTPException(404, "Device not registered")
    dev.last_seen = time.time()
    if battery is not None:
        dev.battery = battery
    if status is not None:
        dev.reported_status = status
    return {"status": "ok"}


@app.get("/api/devices/poll")
async def poll(device_id: str, auth: tuple[str, str] = Depends(device_auth)) -> dict[str, Any]:
    owner, token = auth
    dev = _fleet_of(owner).get(device_id)
    if dev is None:
        raise HTTPException(404, "Device not registered")
    _touch_namespaces(owner, token)  # keeps the org list fresh (TTL-gated; token not kept)
    dev.last_seen = time.time()
    # Long-poll: hold the connection open until a command is enqueued (which
    # sets dev.wakeup) or LONG_POLL_S elapses, so a command is delivered on the
    # next round-trip instead of the next poll interval. Clear the event FIRST,
    # then re-check the queue: an enqueue landing in that gap sets the event, so
    # the wait() returns immediately rather than stranding the command. When
    # LONG_POLL_S<=0 this whole block is skipped → short-poll (return at once).
    if LONG_POLL_S > 0:
        dev.wakeup.clear()
        if not any(c.status == "pending" for c in dev.queue):
            try:
                await asyncio.wait_for(dev.wakeup.wait(), timeout=LONG_POLL_S)
            except asyncio.TimeoutError:
                pass
            # NB: do NOT refresh last_seen here — a held poll that times out on a
            # DEAD device would otherwise look like fresh contact and delay
            # offline detection. last_seen reflects the poll's ARRIVAL (above);
            # liveness is kept fresh by the separate lightweight heartbeat.
    pending = [c for c in dev.queue if c.status == "pending"]
    for c in pending:
        c.status = "sent"
    return {"commands": [{"id": c.id, "type": c.type, "args": c.args} for c in pending]}


@app.post("/api/devices/result")
async def result(req: ResultReq, auth: tuple[str, str] = Depends(device_auth)) -> dict[str, str]:
    owner, _token = auth
    dev = _fleet_of(owner).get(req.device_id)
    if dev is None:
        raise HTTPException(404, "Device not registered")
    for c in dev.queue:
        if c.id == req.command_id:
            c.status = "done"
            c.result = req.result
            c.done_at = time.time()
            dev.queue.remove(c)
            dev.history.insert(0, c)
            del dev.history[20:]
            if c.type == "logout" and dev.pending_delete:
                fleet = _fleet_of(owner)
                fleet.pop(dev.device_id, None)
                _remove_from_groups(owner, dev.device_id)
            break
    # A stop_capture result means that device actually finished tearing down its
    # capture. End the session's "stopping" phase once EVERY dispatched stop has
    # reported — so the UI leaves "stopping" exactly when the devices really stop.
    for s in _sessions_of(owner).values():
        if req.command_id in s.pending_stop_cmds:
            s.pending_stop_cmds.discard(req.command_id)
            if not s.pending_stop_cmds:
                s.stopping = False
            break
    return {"status": "ok"}


# === operator-facing API (session OAuth) =====================================
class DispatchReq(BaseModel):
    device_id: str
    type: str
    args: dict[str, Any] = {}


@app.get("/api/fleet/me")
async def me(request: Request) -> dict[str, Any]:
    info = parse_huggingface_oauth(request)
    if info is None:
        return {"logged_in": False}
    return {"logged_in": True, "username": info.user_info.preferred_username or info.user_info.name}


@app.get("/api/fleet/namespaces")
async def namespaces(request: Request) -> dict[str, Any]:
    """The namespaces the operator can push a dataset to: their own username +
    the orgs they belong to. Used to populate the dataset owner dropdown.

    Served from the cache built when a device token passed through (device tokens
    list orgs reliably; see _touch_namespaces) — so the fleet keeps no raw token.
    Cold start (no device seen yet for this owner): best-effort via the operator's
    OAuth token, which lists orgs poorly, falling back to just the username."""
    owner, oauth_token = operator_auth(request)
    ent = _namespaces_cache.get(owner)
    if ent and ent[1]:
        return {"namespaces": ent[1], "default": owner}
    names = [owner]
    if oauth_token:
        try:
            info = await asyncio.to_thread(whoami, oauth_token)
            names += [o["name"] for o in (info.get("orgs") or []) if o.get("name")]
        except Exception:
            logger.warning("whoami for namespaces failed for %s", owner, exc_info=True)
    seen: set[str] = set()
    uniq = [n for n in names if not (n in seen or seen.add(n))]
    return {"namespaces": uniq, "default": owner}


@app.get("/api/fleet/devices")
async def list_devices(request: Request) -> dict[str, Any]:
    owner = operator_name(request)
    devices = list(_fleet_of(owner).values())
    now = time.time()
    for d in devices:
        # Only drop stale commands for a LONG-gone device — never on the mere
        # 15s online window (would nuke an in-flight group start/stop).
        if now - d.last_seen > STALE_QUEUE_S:
            d.queue.clear()
    return {
        "owner": owner,
        "devices": [
            {
                "device_id": d.device_id,
                "name": d.name,
                "online": d.online,
                "capabilities": d.capabilities,
                "hand": d.hand,
                "ip": d.ip,
                "battery": d.battery,
                "activity": _device_activity(owner, d),
                "pending": len(d.queue),
                "history": [
                    {"type": c.type, "args": c.args, "result": c.result, "ts": c.done_at or c.created_at}
                    for c in d.history[:5]
                ],
            }
            for d in devices
        ],
    }


@app.delete("/api/fleet/devices/{device_id}")
async def remove_device(device_id: str, request: Request) -> dict[str, str]:
    owner = await _operator_loaded(request)
    fleet = _fleet_of(owner)
    if device_id not in fleet:
        raise HTTPException(404, "Device not found")
    del fleet[device_id]
    _remove_from_groups(owner, device_id)
    return {"status": "ok"}


@app.post("/api/fleet/devices/{device_id}/remove")
async def remove_device_graceful(device_id: str, request: Request) -> dict[str, str]:
    """Remove a device: dispatch logout if online (then delete on result), delete immediately if offline."""
    owner = await _operator_loaded(request)
    fleet = _fleet_of(owner)
    dev = fleet.get(device_id)
    if dev is None:
        raise HTTPException(404, "Device not found")
    if dev.online:
        dev.pending_delete = True
        cmd = Command(id=uuid.uuid4().hex[:12], type="logout", args={})
        _enqueue(dev, cmd)
    else:
        fleet.pop(device_id, None)
        _remove_from_groups(owner, device_id)
    return {"status": "ok"}


# === tasks (operator-facing, session OAuth) ==================================
class TaskReq(BaseModel):
    name: str = ""
    description: str = ""
    device_signature: list[str] = []


def _task_dict(t: Task) -> dict[str, Any]:
    return {"id": t.id, "name": t.name, "description": t.description,
            "device_signature": t.device_signature}


@app.get("/api/fleet/tasks")
async def list_tasks(request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    _reconcile_tasks(owner)  # surface tasks reported by connected devices
    agg = _reported_tasks(owner)
    reporters = _online_episode_reporters(owner)  # for repairing incomplete episodes
    out = []
    for t in _tasks_of(owner).values():
        d = _task_dict(t)
        eps = _episodes_from_entry(agg.get(t.name))
        sig = set(t.device_signature or [])
        for ep in eps:
            # Roles we actually know a device for. Incomplete = the task's expected
            # roles aren't all covered (or none is), e.g. episodes recorded before
            # the device stored its members. Fillable = a connected device that
            # reports this episode could supply a role we're missing.
            present = {r for r, w in ep["members"].items() if w.get("device_id")}
            incomplete = (bool(sig) and not sig.issubset(present)) or not present
            recon = reporters.get(ep["episode_id"], {})
            ep["incomplete"] = incomplete
            ep["fillable"] = incomplete and any(r not in present for r in recon)
        d["episodes"] = eps
        d["episode_count"] = len(eps)
        out.append(d)
    return {"tasks": out}


@app.post("/api/fleet/tasks/{task_id}/fill-devices")
async def fill_task_devices(task_id: str, request: Request) -> dict[str, Any]:
    """Repair ALL of a task's incomplete episodes at once (recorded before the
    device persisted their members): reconstruct role → device from the CONNECTED
    devices that still report each episode, then persist onto them so the fleet's
    view — and the devices themselves — become the durable source of truth.
    One batched command per device (not per episode). Partial by design: fills
    only the roles whose devices are online now, so a re-run completes the rest."""
    owner = await _operator_loaded(request)
    task = _tasks_of(owner).get(task_id)
    if task is None:
        raise HTTPException(404, "Task not found")
    eps = _episodes_from_entry(_reported_tasks(owner).get(task.name))
    sig = set(task.device_signature or [])
    # One pass: episode_id → online devices that still report it.
    ep_devs: dict[str, list[Device]] = {}
    for dev in _fleet_of(owner).values():
        if not dev.online:
            continue
        seen: set[str] = set()
        for t in dev.tasks or []:
            for eid, _m in _task_episode_items(t):
                if eid and eid not in seen:
                    seen.add(eid)
                    ep_devs.setdefault(eid, []).append(dev)
    # Build one batch of (episode_id, reconstructed members) per target device.
    batches: dict[str, dict] = {}  # device_id → {"dev": Device, "entries": [...]}
    filled: set[str] = set()
    for ep in eps:
        eid = ep["episode_id"]
        present = {r for r, w in ep["members"].items() if w.get("device_id")}
        incomplete = (bool(sig) and not sig.issubset(present)) or not present
        if not incomplete:
            continue
        devs = ep_devs.get(eid, [])
        recon: dict[str, dict] = {}
        for dev in devs:
            role = _device_slot(dev)
            if role:
                recon.setdefault(role, {"device_id": dev.device_id, "name": dev.name})
        if not any(r not in present for r in recon):  # nothing new we can add now
            continue
        filled.add(eid)
        for dev in devs:
            batches.setdefault(dev.device_id, {"dev": dev, "entries": []})["entries"].append(
                {"episode_id": eid, "members": recon})
    if not batches:
        raise HTTPException(409, {"message": "no incomplete episode can be repaired right now — "
                                             "connect the grabettes that recorded them, then retry"})
    for b in batches.values():
        cmd = Command(id=uuid.uuid4().hex[:12], type="set_episode_members",
                      args={"task_name": task.name, "episodes": b["entries"],
                            "device_signature": list(task.device_signature)})
        _enqueue(b["dev"], cmd)
    return {"status": "ok", "episodes": len(filled),
            "devices": [b["dev"].device_id for b in batches.values()]}


def _clean_signature(sig: list[str]) -> list[str]:
    bad = [s for s in sig if s not in VALID_SLOTS]
    if bad:
        raise HTTPException(400, f"Invalid device_signature entries: {bad}; allowed: {list(VALID_SLOTS)}")
    # de-dup, canonical order
    cleaned = [s for s in VALID_SLOTS if s in sig]
    if not cleaned:
        raise HTTPException(400, "A task must require at least one device")
    return cleaned


@app.post("/api/fleet/tasks")
async def create_task(req: TaskReq, request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    if not req.name.strip():
        raise HTTPException(400, "Task needs a name")
    tasks = _tasks_of(owner)
    if any(t.name == req.name.strip() for t in tasks.values()):
        raise HTTPException(400, "A task with this name already exists")
    tid = uuid.uuid4().hex[:8]
    tasks[tid] = Task(id=tid, name=req.name.strip(), description=req.description.strip(),
                      device_signature=_clean_signature(req.device_signature))
    return {"status": "ok", "id": tid, "task": _task_dict(tasks[tid])}


@app.put("/api/fleet/tasks/{task_id}")
async def update_task(task_id: str, req: TaskReq, request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    _reconcile_tasks(owner)
    t = _tasks_of(owner).get(task_id)
    if t is None:
        raise HTTPException(404, "Task not found")
    old_name = t.name
    new_name = req.name.strip() or old_name
    new_desc = req.description.strip()
    new_sig = _clean_signature(req.device_signature)

    # The device signature is locked ONCE it's known and episodes exist — the
    # recordings were made with that device set, so changing it would misdescribe
    # them. But an old task with episodes and NO recorded signature can be set
    # once (to backfill it); after that it's known → locked.
    has_episodes = bool(_episodes_from_entry(_reported_tasks(owner).get(old_name)))
    sig_locked = has_episodes and bool(t.device_signature)
    if sig_locked and sorted(new_sig) != sorted(t.device_signature):
        raise HTTPException(409, {"message": "cannot change the required devices of a task "
                                             "that already has recorded episodes"})
    if new_name != old_name and any(o.name == new_name for o in _tasks_of(owner).values()):
        raise HTTPException(400, "A task with this name already exists")

    sig_changed = (not sig_locked) and sorted(new_sig) != sorted(t.device_signature)

    # Apply on the fleet, then propagate to every device that recorded this task
    # (the source of truth) so the change survives the next report reconcile.
    t.name = new_name
    t.description = new_desc
    if not sig_locked:
        t.device_signature = new_sig
    for dev in _devices_reporting_task(owner, old_name):
        args = {"name": old_name,
                "new_name": new_name if new_name != old_name else None,
                "description": new_desc}
        if sig_changed:  # backfilling an old task's signature → devices must store it
            args["device_signature"] = new_sig
        _enqueue(dev, Command(id=uuid.uuid4().hex[:12], type="edit_task", args=args))
    if new_name != old_name:
        _suppress_task_name(owner, old_name)  # hide stale old-name reports until re-report
    return {"status": "ok", "task": _task_dict(t)}


@app.delete("/api/fleet/tasks/{task_id}")
async def delete_task(task_id: str, request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    _reconcile_tasks(owner)
    tasks = _tasks_of(owner)
    t = tasks.get(task_id)
    if t is None:
        raise HTTPException(404, "Task not found")
    if any(s.status == "open" and s.task_id == task_id for s in _sessions_of(owner).values()):
        raise HTTPException(409, "Cannot delete a task with an open session")
    name = t.name
    # Delete on every ONLINE device that has this task — the task AND its recorded
    # episodes go (the device is the source of truth; removing here alone would let
    # the next report resurrect it). Offline members keep their copy for now; they
    # are reconciled when they reconnect (see orphan detection, phase 3).
    dispatched = []
    for dev in _devices_reporting_task(owner, name):
        _enqueue(dev, Command(id=uuid.uuid4().hex[:12], type="delete_task", args={"name": name}))
        dispatched.append(dev.device_id)
    del tasks[task_id]
    _suppress_task_name(owner, name)  # hide it until the devices re-report without it
    return {"status": "ok", "dispatched": dispatched}


class OrphanCleanupReq(BaseModel):
    episode_ids: list[str] = []


@app.get("/api/fleet/orphans")
async def list_orphans(request: Request) -> dict[str, Any]:
    """Orphaned episodes (a peer deleted them but a connected device still holds
    them), grouped by task for the reconciliation banner. Returns the cached
    snapshot computed on the last device register — not a per-poll recompute."""
    owner = await _operator_loaded(request)
    reported = _reported_tasks(owner)
    groups: dict[str, dict] = {}
    for o in ORPHANS_PENDING.get(owner, []):
        g = groups.setdefault(o["task"], {"task": o["task"], "episode_ids": [],
                                          "holders": {}, "deleted_by": {}})
        g["episode_ids"].append(o["episode_id"])
        for h in o["holders"]:
            g["holders"][h["device_id"]] = h["name"]
        for d in o["deleted_by"]:
            g["deleted_by"][d["device_id"]] = d["name"]
    out = []
    for g in groups.values():
        # If EVERY remaining episode of the task is orphaned, the whole task was
        # deleted on the peer(s) — say so, rather than just "episodes lost pairs".
        entry = reported.get(g["task"])
        total = len(entry["episodes"]) if entry else len(g["episode_ids"])
        out.append({"task": g["task"], "episode_ids": g["episode_ids"],
                    "count": len(g["episode_ids"]),
                    "whole_task": len(g["episode_ids"]) >= total,
                    "holders": list(g["holders"].values()),
                    "deleted_by": list(g["deleted_by"].values())})
    return {"groups": out}


@app.post("/api/fleet/orphans/cleanup")
async def cleanup_orphans(req: OrphanCleanupReq, request: Request) -> dict[str, Any]:
    """Delete the given orphaned episodes on the devices still holding them.
    Recomputes orphans so only genuinely-orphaned episodes are touched."""
    owner = await _operator_loaded(request)
    # Recompute live here (accuracy matters for a destructive action).
    orphans = {o["episode_id"]: o for o in _orphan_episodes(owner)}
    fleet = _fleet_of(owner)
    dispatched = 0
    cleaned = set()
    for eid in req.episode_ids:
        o = orphans.get(eid)
        if not o:
            continue
        for h in o["holders"]:
            dev = fleet.get(h["device_id"])
            if dev is not None and dev.online:
                _enqueue(dev, Command(id=uuid.uuid4().hex[:12], type="delete_episode",
                                      args={"episode_id": eid}))
                dispatched += 1
                cleaned.add(eid)
    # Drop the cleaned episodes from the cached snapshot so the banner clears at
    # once; the holders' re-register will confirm on the next report.
    if cleaned and owner in ORPHANS_PENDING:
        ORPHANS_PENDING[owner] = [o for o in ORPHANS_PENDING[owner] if o["episode_id"] not in cleaned]
    return {"status": "ok", "dispatched": dispatched}


# === device groups (operator-facing, session OAuth) ==========================
class GroupReq(BaseModel):
    name: str = ""
    left: str = ""
    right: str = ""
    casquette: str = ""


def _validate_group_members(owner: str, req: GroupReq, group_id: Optional[str] = None) -> None:
    fleet = _fleet_of(owner)
    groups = _groups_of(owner)
    slots = {"left": req.left, "right": req.right, "casquette": req.casquette}
    if not any(slots.values()):
        raise HTTPException(400, "Group needs at least one device")
    for slot, device_id in slots.items():
        if not device_id:
            continue
        dev = fleet.get(device_id)
        if dev is None:
            raise HTTPException(404, f"Device {device_id} not found")
        if slot in ("left", "right"):
            if kind_of(dev) != "grabette" or dev.hand != slot:
                raise HTTPException(400, f"Device {device_id} is not a {slot}-hand grabette")
        elif kind_of(dev) != "casquette":
            raise HTTPException(400, f"Device {device_id} is not a casquette")
        for gid, g in groups.items():
            if gid == group_id:
                continue
            if device_id in (g.left, g.right, g.casquette):
                raise HTTPException(400, f"Device {device_id} is already in another group")


@app.get("/api/fleet/groups")
async def list_groups(request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    groups = list(_groups_of(owner).values())
    return {
        "groups": [
            {"id": g.id, "name": g.name, "left": g.left, "right": g.right, "casquette": g.casquette}
            for g in groups
        ]
    }


@app.post("/api/fleet/groups")
async def create_group(req: GroupReq, request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    _validate_group_members(owner, req)
    groups = _groups_of(owner)
    gid = uuid.uuid4().hex[:8]
    groups[gid] = Group(
        id=gid, name=req.name.strip() or f"Group {len(groups) + 1}", left=req.left, right=req.right, casquette=req.casquette,
    )
    return {"status": "ok", "id": gid}


@app.put("/api/fleet/groups/{group_id}")
async def update_group(group_id: str, req: GroupReq, request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    groups = _groups_of(owner)
    g = groups.get(group_id)
    if g is None:
        raise HTTPException(404, "Group not found")
    _validate_group_members(owner, req, group_id=group_id)
    g.name = req.name.strip() or g.name
    g.left, g.right, g.casquette = req.left, req.right, req.casquette
    return {"status": "ok"}


@app.delete("/api/fleet/groups/{group_id}")
async def delete_group(group_id: str, request: Request) -> dict[str, str]:
    owner = await _operator_loaded(request)
    groups = _groups_of(owner)
    if group_id not in groups:
        raise HTTPException(404, "Group not found")
    del groups[group_id]
    return {"status": "ok"}


def _group_members(g: Group) -> list[str]:
    return [d for d in (g.left, g.right, g.casquette) if d]


def _lead_for(session: Session) -> float:
    """Pick the start lead for the next episode: short if the OAK-D is still
    warm (a recent episode stopped within OAK_WARM_WINDOW_S), long otherwise
    (first episode of the session, or the OAK-D has since gone to sleep) so the
    cold boot finishes before T0. Conservative: unknown/old → cold."""
    if session.last_stop_at is not None and (time.time() - session.last_stop_at) < OAK_WARM_WINDOW_S:
        return GROUP_START_LEAD_WARM_S
    return GROUP_START_LEAD_COLD_S


def _named_members(owner: str, members: dict[str, str]) -> dict[str, dict[str, str]]:
    """role → {device_id, name}. Sent to devices so each records who its peers
    were (by stable device_id + display name), letting a device name an offline
    peer later — the device is the durable source of truth for episode membership."""
    fleet = _fleet_of(owner)
    return {
        r: {"device_id": d, "name": (fleet[d].name if d in fleet else d)}
        for r, d in members.items()
    }


def _schedule_episode_start(
    owner: str, task_name: str, members: dict[str, str], lead_s: float,
    exclude_device_id: Optional[str] = None, signature: Optional[list[str]] = None,
) -> str:
    """Enqueue a synchronized start_capture to every member device (except
    exclude_device_id, which self-schedules from the returned T0). Raises 409
    if any member is offline. Returns the shared start_at_utc (ISO).

    Each command carries the episode's full membership (role → device_id+name)
    and the task's device signature so every device persists who recorded with
    it — the fleet no longer needs its own HF-backed record of this."""
    fleet = _fleet_of(owner)
    device_ids = list(members.values())
    offline = [d for d in device_ids if d not in fleet or not fleet[d].online]
    if offline:
        raise HTTPException(409, {"message": "one or more devices are offline", "offline": offline})

    named = _named_members(owner, members)
    target_iso = (datetime.now(timezone.utc) + timedelta(seconds=lead_s)).isoformat()
    for device_id in device_ids:
        if device_id == exclude_device_id:
            continue
        cmd = Command(
            id=uuid.uuid4().hex[:12], type="start_capture",
            args={"task_name": task_name, "start_at_utc": target_iso,
                  "members": named, "signature": signature or []},
        )
        _enqueue(fleet[device_id], cmd)
    return target_iso


def _dispatch_episode_stop(owner: str, members: dict[str, str], exclude_device_id: Optional[str] = None) -> list[str]:
    """Fan out an IMMEDIATE stop to every member device except exclude_device_id
    (the acting device, which has already stopped locally). Each peer stops as
    soon as it receives the command, i.e. within ~1 poll interval — the pressed
    device is instant and peers trail only by the short delivery latency, no
    scheduled lead. Chaining fast stays safe because the next episode's start
    still uses a lead (GROUP_START_LEAD_*) that the peer's stop+mux fits inside.

    Returns the dispatched stop command ids so the caller can wait for their
    results (see Session.pending_stop_cmds) and end the "stopping" phase exactly
    when the devices report done, rather than after a fixed guess.

    (A synchronized, lead-based stop — shared future T_stop via GROUP_STOP_LEAD_S
    + the device-side CaptureScheduler.schedule_stop path — remains wired up but
    unused here; it'd matter only if delivery latency grew. With long-polling
    delivery drops to a network round-trip, making even this spread negligible.)"""
    fleet = _fleet_of(owner)
    ids: list[str] = []
    for device_id in members.values():
        if device_id == exclude_device_id:
            continue
        dev = fleet.get(device_id)
        if dev is not None:
            cmd = Command(id=uuid.uuid4().hex[:12], type="stop_capture", args={})
            _enqueue(dev, cmd)
            ids.append(cmd.id)
    return ids


def _begin_episode_stop(s: "Session", stop_cmd_ids: list[str]) -> None:
    """Mark a session as no longer recording and enter the 'stopping' phase,
    awaiting the dispatched stops' results (see Session.stopping). Only claims
    'stopping' when there are commands to wait on — a stop with no tracked device
    (e.g. a solo device that stopped locally) goes straight to idle."""
    s.recording = False
    s.last_stop_at = time.time()  # also drives the warm/cold lead for the next episode
    s.pending_stop_cmds = set(stop_cmd_ids)
    s.stopping = bool(stop_cmd_ids)


def _episode_id_for_target(target_iso: str) -> str:
    """Mirror the device's episode_id_for(T0): same UTC second → same id, so
    the manifest entry matches the folder every device actually creates."""
    return datetime.fromisoformat(target_iso).astimezone(timezone.utc).strftime("%Y%m%d_%H%M%S")


# === sessions (operator-facing, session OAuth) ===============================
# The recording unit. Launch = pick a task + a target (a group, or a single
# device treated as a group of one), validate the target's roles match the
# task's required devices, then record one or more synchronized episodes into
# the session's manifest, and close it.
class SessionLaunchReq(BaseModel):
    task_id: str
    # A device id per role, picked from the fleet at launch time. The device
    # selection IS the grouping — no Group object is created (the session owns
    # its members map).
    left: str = ""
    right: str = ""
    casquette: str = ""


def _resolve_members(owner: str, req: SessionLaunchReq) -> dict[str, str]:
    """Build the session's role→device map from the per-role device ids,
    checking each device actually fills the role it was placed in."""
    fleet = _fleet_of(owner)
    members: dict[str, str] = {}
    for role, device_id in (("left", req.left), ("right", req.right), ("casquette", req.casquette)):
        if not device_id:
            continue
        dev = fleet.get(device_id)
        if dev is None:
            raise HTTPException(404, f"Device {device_id} not found")
        if _device_slot(dev) != role:
            raise HTTPException(400, f"Device {device_id} cannot fill the {role} role")
        if not dev.online:
            # A session can't be launched with an offline device — it couldn't
            # receive the synchronized start and would fail at the first episode.
            raise HTTPException(409, {"message": f"{dev.name} is offline", "offline": [device_id]})
        members[role] = device_id
    if not members:
        raise HTTPException(400, "Select at least one device")
    return members


# Safety cap: if a device never reports its stop result (e.g. it dropped offline
# mid-stop), don't leave the "stopping" phase stuck — expose it as ended after this.
STOP_PHASE_MAX_S = 30.0


def _session_dict(owner: str, s: Session) -> dict[str, Any]:
    fleet = _fleet_of(owner)
    def dname(d):
        dev = fleet.get(d)
        return dev.name if dev else d
    # "stopping" ends when every dispatched stop reported (s.stopping cleared);
    # the time cap is only a fallback for a device that never reports.
    stopping = s.stopping and s.last_stop_at is not None and (time.time() - s.last_stop_at) < STOP_PHASE_MAX_S
    return {
        "id": s.id, "status": s.status, "task_id": s.task_id,
        "task_name": _task_name(owner, s.task_id),
        "recording": s.recording,
        "stopping": stopping,  # accurate "stopping" phase — ends when the devices really stop
        "started_at": s.started_at,
        "members": {r: {"device_id": d, "name": dname(d)} for r, d in s.members.items()},
        "episode_count": len(s.episodes),
        "episodes": s.episodes,
    }


@app.get("/api/fleet/sessions")
async def list_sessions(request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    return {"sessions": [_session_dict(owner, s) for s in _sessions_of(owner).values()]}


@app.post("/api/fleet/sessions")
async def launch_session(req: SessionLaunchReq, request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    task = _tasks_of(owner).get(req.task_id)
    if task is None:
        raise HTTPException(404, "Task not found")
    members = _resolve_members(owner, req)
    # The target must provide EXACTLY the roles the task requires.
    _validate_task_signature(owner, req.task_id, set(members.keys()))
    # A single open session at a time (fleet-wide). Launching is done from the
    # task list; the running one must be closed before another can start.
    if any(s.status == "open" for s in _sessions_of(owner).values()):
        raise HTTPException(409, {"message": "a session is already running; close it first"})
    # A device tied up by a dataset upload/conversion can't also record.
    busy = _busy_recording_blockers(owner, members.values())
    if busy:
        fleet = _fleet_of(owner)
        names = ", ".join(fleet[d].name for d in busy if d in fleet) or "a device"
        raise HTTPException(409, {"message": f"{names} is busy processing a dataset — wait for it to finish",
                                  "devices": busy})
    sid = uuid.uuid4().hex[:8]
    _sessions_of(owner)[sid] = Session(id=sid, task_id=req.task_id, members=members)
    # No warm-up on launch: keeping the OAK-D on until the first episode would
    # drain the battery if the operator waits. Instead the first episode uses
    # the COLD lead (long enough for the scheduler to warm the OAK-D during the
    # lead), and after that the device keepalive keeps it warm between episodes.
    return {"status": "ok", "id": sid, "session": _session_dict(owner, _sessions_of(owner)[sid])}


def _get_open_session(owner: str, session_id: str) -> Session:
    s = _sessions_of(owner).get(session_id)
    if s is None:
        raise HTTPException(404, "Session not found")
    if s.status != "open":
        raise HTTPException(409, "Session is closed")
    return s


@app.post("/api/fleet/sessions/{session_id}/episode/start")
async def session_episode_start(session_id: str, request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    s = _get_open_session(owner, session_id)
    # A member pulled into a dataset upload/conversion can't start a new episode.
    busy = _busy_recording_blockers(owner, s.members.values())
    if busy:
        fleet = _fleet_of(owner)
        names = ", ".join(fleet[d].name for d in busy if d in fleet) or "a device"
        raise HTTPException(409, {"message": f"{names} is busy processing a dataset — wait for it to finish",
                                  "devices": busy})
    _task = _tasks_of(owner).get(s.task_id)
    target_iso = _schedule_episode_start(
        owner, _task_name(owner, s.task_id), s.members, _lead_for(s),
        signature=_task.device_signature if _task else [],
    )
    episode_id = _episode_id_for_target(target_iso)
    # Record the manifest entry up front from the deterministic id; per-device
    # success is reconciled later via /api/devices/result (phase 3). start_at_utc
    # is the shared T0 — the UI shows "initializing" until then, "recording" after.
    s.episodes.append({"episode_id": episode_id, "roles": dict(s.members),
                       "started_at": datetime.now(timezone.utc).isoformat(),
                       "start_at_utc": target_iso})
    s.recording = True
    return {"status": "scheduled", "scheduled_start_utc": target_iso, "episode_id": episode_id,
            "devices": list(s.members.values())}


@app.post("/api/fleet/sessions/{session_id}/episode/stop")
async def session_episode_stop(session_id: str, request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    s = _get_open_session(owner, session_id)
    ids = _dispatch_episode_stop(owner, s.members)
    _begin_episode_stop(s, ids)
    return {"status": "ok", "devices": list(s.members.values())}


@app.post("/api/fleet/sessions/{session_id}/episode/delete-last")
async def session_delete_last_episode(session_id: str, request: Request) -> dict[str, Any]:
    """Drop the session's most recent episode from the manifest and tell each
    device that recorded it to delete its local files. Refused while recording
    (the last manifest entry is then the in-progress episode)."""
    owner = await _operator_loaded(request)
    s = _get_open_session(owner, session_id)
    if s.recording:
        raise HTTPException(409, {"message": "stop the current episode before deleting"})
    if not s.episodes:
        raise HTTPException(409, {"message": "no episode to delete"})
    ep = s.episodes.pop()  # most recent
    episode_id = ep["episode_id"]
    fleet = _fleet_of(owner)
    devices = list((ep.get("roles") or {}).values())
    for dev_id in devices:
        dev = fleet.get(dev_id)
        if dev is not None:
            _enqueue(dev, Command(id=uuid.uuid4().hex[:12], type="delete_episode",
                                  args={"episode_id": episode_id}))
    return {"status": "ok", "episode_id": episode_id, "devices": devices}


@app.post("/api/fleet/sessions/{session_id}/close")
async def close_session(session_id: str, request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    s = _sessions_of(owner).get(session_id)
    if s is None:
        raise HTTPException(404, "Session not found")
    # Best-effort: make sure nothing is left recording on the members.
    _dispatch_episode_stop(owner, s.members)
    if not s.episodes:
        # A session that recorded nothing isn't worth keeping — drop it so it
        # never clutters the task's history.
        del _sessions_of(owner)[session_id]
        return {"status": "ok", "discarded": True}
    s.recording = False
    s.last_stop_at = time.time()
    s.status = "closed"
    return {"status": "ok"}


@app.delete("/api/fleet/sessions/{session_id}")
async def delete_session(session_id: str, request: Request) -> dict[str, str]:
    owner = await _operator_loaded(request)
    if session_id not in _sessions_of(owner):
        raise HTTPException(404, "Session not found")
    del _sessions_of(owner)[session_id]
    return {"status": "ok"}


# === LeRobot dataset generation (operator OAuth) ============================
# Selected tasks (all sharing one device signature) → each involved device
# pushes its own streams to a shared raw dataset (by role) → a processing Space
# converts raw → LeRobot. The fleet only orchestrates; devices upload with their
# own write tokens (the fleet can't reach them, and needs no token for uploads).
class DatasetReq(BaseModel):
    task_ids: list[str]
    name: str = ""  # target dataset name; bare name → {owner}/{name}
    private: bool = False  # create the resulting LeRobot dataset as private
    # If True, include only episodes whose EVERY role-device is currently online
    # (skip episodes that reference an offline device) instead of requiring the
    # whole set of devices to be up.
    only_available: bool = False
    # "Use only" advanced option: a subset of roles (e.g. ["left"] or
    # ["left","casquette"]). When set, ANY task whose signature is a superset is
    # eligible, and only these roles' devices are uploaded → build a dataset from
    # just this sub-combination. Empty → use each selected task's full signature.
    roles: list[str] = []


def _command_status(dev: Device, cmd_id: str) -> Optional[Command]:
    """Find a dispatched command by id, whether still queued or completed
    (results move a command from queue to history)."""
    for c in list(dev.queue) + list(dev.history):
        if c.id == cmd_id:
            return c
    return None


def _resolve_dataset_plan(owner: str, task_ids: list[str], only_available: bool = False,
                          roles_override: Optional[list[str]] = None) -> tuple[list[str], dict[str, dict]]:
    """Validate the selection and build the per-device upload plan from the
    device reports. Returns (roles, plan) where plan maps device_id ->
    {"role": role, "episode_ids": set[str]}.

    roles_override ("Use only"): a subset of roles. When given, every selected
    task must merely CONTAIN these roles (superset), and only these roles' devices
    are uploaded — so you can build, say, a left-only dataset from bimanual tasks.
    When absent, the selected tasks must share one signature and all its roles are
    used.

    only_available: skip episodes that reference a device which is currently
    offline — the resulting dataset covers only what can be fully uploaded now."""
    _reconcile_tasks(owner)
    tasks = _tasks_of(owner)
    sel = [tasks[t] for t in task_ids if t in tasks]
    if not sel:
        raise HTTPException(404, "no matching tasks")
    override = [r for r in (roles_override or []) if r in VALID_SLOTS]
    if override:
        oset = set(override)
        bad = [t.name for t in sel if not oset.issubset(set(t.device_signature))]
        if bad:
            raise HTTPException(409, {"message": "some selected tasks don't have the chosen devices",
                                      "tasks": bad})
        roles = sorted(oset)
    else:
        sigs = {tuple(sorted(t.device_signature)) for t in sel}
        if len(sigs) != 1:
            raise HTTPException(409, {"message": "selected tasks must share the same device signature"})
        roles = sorted(sel[0].device_signature)
    agg = _reported_tasks(owner)
    fleet = _fleet_of(owner)

    def _online(d):
        return d in fleet and fleet[d].online

    # Episodes come from the devices' reports (the source of truth), matched by
    # task name. members' device_id per role tells us which device to ask to
    # upload that role's data for each episode. With an override, restrict to
    # just those roles so only the requested devices upload.
    role_set = set(roles)
    plan: dict[str, dict] = {}
    for t in sel:
        for ep in _episodes_from_entry(agg.get(t.name)):
            roles_map = {r: d for r, d in ep["roles"].items() if r in role_set}
            if not roles_map:
                continue  # this episode lacks the requested roles
            if only_available and not all(_online(d) for d in roles_map.values()):
                continue  # a needed device is offline → episode unusable
            for role, dev_id in roles_map.items():
                if not dev_id:
                    continue
                p = plan.setdefault(dev_id, {"role": role, "episode_ids": set()})
                p["episode_ids"].add(ep["episode_id"])
    if not plan:
        msg = ("no episodes have all their devices online"
               if only_available else "selected tasks have no recorded episodes")
        raise HTTPException(409, {"message": msg})
    return roles, plan


async def _run_dataset_job(owner: str, job: DatasetJob, private: bool,
                           processor_device_id: str) -> None:
    """Background: wait for every device's upload to complete, then have ONE
    device trigger the processing Space (raw → LeRobot, mono/bimanual per the
    device set) with ITS OWN long-lived token and report the result back.

    The fleet never handles an HF token for the Space call: the device→Space
    channel is the same one the SLAM flow already uses, so no token is cached on
    the fleet nor forwarded through it (avoids widening the token's blast radius
    and the short-lived-OAuth-token expiry problem). The Space downloads the raw,
    builds + pushes the dataset, and deletes the raw itself."""
    fleet = _fleet_of(owner)
    deadline = time.time() + 1800.0  # 30-min cap for the whole upload phase
    try:
        pending = dict(job.upload_cmds)  # device_id -> command id
        n = len(pending)
        while pending:
            if time.time() > deadline:
                raise RuntimeError("upload timed out")
            await asyncio.sleep(2.0)
            for dev_id, cmd_id in list(pending.items()):
                dev = fleet.get(dev_id)
                c = _command_status(dev, cmd_id) if dev else None
                if c is not None and c.status == "done":
                    res = c.result or {}
                    if res.get("status") != "ok":
                        raise RuntimeError(f"{(dev.name if dev else dev_id)}: {res.get('message', 'upload failed')}")
                    pending.pop(dev_id)
                    job.progress = (n - len(pending)) / n if n else 1.0
                    job.message = f"Uploaded {n - len(pending)}/{n} device(s)… (this can take several minutes)"
                    continue
                # Not done yet — bail fast if the device dropped offline (it went
                # dark before finishing, so its upload will never complete). An
                # actively-uploading device keeps polling → stays online, so this
                # only fires on a real disconnect (within ~ONLINE_WINDOW).
                if dev is None or not dev.online:
                    who = dev.name if dev else dev_id
                    raise RuntimeError(f"device '{who}' went offline before finishing its upload — reconnect it and retry")

        # Raw dataset complete → one device runs the processing (calls the Space
        # with its own token; the command completes when the Space is done).
        job.status = "processing"
        job.progress = None  # opaque Space conversion → indeterminate bar
        job.message = "Converting to LeRobot… (this can take several minutes)"
        dev = fleet.get(processor_device_id)
        if dev is None or not dev.online:
            raise RuntimeError("no online device available to run processing")
        proc = Command(id=uuid.uuid4().hex[:12], type="process_dataset",
                       args={"space_url": LEROBOT_SPACE_URL, "source_repo": job.raw_repo,
                             "target_repo": job.target_repo, "roles": job.roles,
                             "private": private, "task": job.target_repo.split("/")[-1]})
        _enqueue(dev, proc)
        proc_deadline = time.time() + 3600.0  # 60-min cap for processing
        while True:
            if time.time() > proc_deadline:
                raise RuntimeError("processing timed out")
            await asyncio.sleep(3.0)
            c = _command_status(dev, proc.id)
            if c is None or c.status != "done":
                # Bail fast if the processing device dropped offline mid-run
                # (it keeps polling while working, so this is a real disconnect).
                d = fleet.get(processor_device_id)
                if d is None or not d.online:
                    who = d.name if d else processor_device_id
                    raise RuntimeError(f"device '{who}' went offline during processing — reconnect it and retry")
                continue
            res = c.result or {}
            if res.get("status") != "ok":
                raise RuntimeError(res.get("message", "processing failed"))
            job.result_url = res.get("result_url") or f"https://huggingface.co/datasets/{job.target_repo}"
            job.status, job.message, job.progress = "done", "Dataset ready.", 1.0
            return
    except Exception as e:  # noqa: BLE001
        job.status, job.error = "error", str(e)
        job.message = f"Failed: {e}"
        logger.warning("dataset job %s failed: %s", job.id, e, exc_info=True)


@app.post("/api/fleet/lerobot-dataset")
async def create_lerobot_dataset(req: DatasetReq, request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    roles, plan = _resolve_dataset_plan(owner, req.task_ids, req.only_available, req.roles or None)
    fleet = _fleet_of(owner)
    # With only_available the plan already excludes offline-device episodes; this
    # guard then bites only in the default mode (require the whole set up).
    offline = [d for d in plan if d not in fleet or not fleet[d].online]
    if offline:
        raise HTTPException(409, {"message": "some recording devices are offline", "devices": offline})
    # One member device runs the processing (calls the Space with its own token).
    processor = next(iter(plan))
    job_id = uuid.uuid4().hex[:8]
    name = (req.name or f"grabette-dataset-{job_id}").strip()
    target_repo = name if "/" in name else f"{owner}/{name}"
    # Intermediate raw dataset named after the target (…-raw) instead of random,
    # so it's easy to spot and correlate. Same namespace as the target.
    raw_repo = f"{target_repo}-raw"
    job = DatasetJob(id=job_id, task_ids=list(req.task_ids), roles=roles,
                     raw_repo=raw_repo, target_repo=target_repo,
                     message=f"Uploading episodes from {len(plan)} device(s)… (this can take several minutes)")
    _dataset_jobs_of(owner)[job_id] = job
    for dev_id, p in plan.items():
        cmd = Command(id=uuid.uuid4().hex[:12], type="upload_episodes",
                      args={"raw_repo": raw_repo, "role": p["role"],
                            "episode_ids": sorted(p["episode_ids"]),
                            "private": req.private})
        _enqueue(fleet[dev_id], cmd)
        job.upload_cmds[dev_id] = cmd.id
    t = asyncio.create_task(_run_dataset_job(owner, job, req.private, processor))
    _bg_tasks.add(t)
    t.add_done_callback(_bg_tasks.discard)
    return {"status": "ok", "job_id": job_id, "raw_repo": raw_repo,
            "target_repo": target_repo, "roles": roles, "devices": list(plan.keys())}


@app.get("/api/fleet/lerobot-dataset/{job_id}")
async def lerobot_dataset_status(job_id: str, request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    job = _dataset_jobs_of(owner).get(job_id)
    if job is None:
        raise HTTPException(404, "job not found")
    return {"id": job.id, "status": job.status, "message": job.message,
            "progress": job.progress,
            "raw_repo": job.raw_repo, "target_repo": job.target_repo,
            "result_url": job.result_url, "error": job.error}


# === device-facing sync API (Bearer auth) — physical-button episode start/stop
# A button press records an episode into the OPEN session containing this
# device (found by membership). It behaves exactly like the operator starting
# an episode from the dashboard: same task, same synchronized T0 across the
# session's members. No open session → solo (the device records locally). ======
@app.post("/api/devices/{device_id}/sync/start")
async def device_sync_start(device_id: str, auth: tuple[str, str] = Depends(device_auth)) -> dict[str, Any]:
    owner, _token = auth
    if device_id not in _fleet_of(owner):
        raise HTTPException(404, "Device not registered")
    s = _open_session_for_device(owner, device_id)
    if s is None:
        return {"status": "solo"}
    task_name = _task_name(owner, s.task_id)
    _task = _tasks_of(owner).get(s.task_id)
    signature = _task.device_signature if _task else []
    target_iso = _schedule_episode_start(
        owner, task_name, s.members, _lead_for(s),
        exclude_device_id=device_id, signature=signature,
    )
    episode_id = _episode_id_for_target(target_iso)
    s.episodes.append({"episode_id": episode_id, "roles": dict(s.members),
                       "started_at": datetime.now(timezone.utc).isoformat(),
                       "start_at_utc": target_iso})
    s.recording = True
    peers = [d for d in s.members.values() if d != device_id]
    # Full membership + signature so the pressing device persists who it recorded
    # with (it self-schedules from scheduled_start_utc, so it's excluded above).
    return {"status": "scheduled", "scheduled_start_utc": target_iso, "task_name": task_name,
            "peers": peers, "members": _named_members(owner, s.members), "signature": signature}


@app.post("/api/devices/{device_id}/sync/stop")
async def device_sync_stop(device_id: str, auth: tuple[str, str] = Depends(device_auth)) -> dict[str, Any]:
    owner, _token = auth
    if device_id not in _fleet_of(owner):
        raise HTTPException(404, "Device not registered")
    s = _open_session_for_device(owner, device_id)
    if s is None:
        return {"status": "solo"}
    ids = _dispatch_episode_stop(owner, s.members, exclude_device_id=device_id)
    _begin_episode_stop(s, ids)
    return {"status": "ok", "peers": [d for d in s.members.values() if d != device_id]}


@app.post("/api/fleet/dispatch")
async def dispatch(req: DispatchReq, request: Request) -> dict[str, Any]:
    owner = await _operator_loaded(request)
    dev = _fleet_of(owner).get(req.device_id)
    if dev is None:
        raise HTTPException(404, "Device not found in your fleet")
    cmd = Command(id=uuid.uuid4().hex[:12], type=req.type, args=dict(req.args))
    _enqueue(dev, cmd)
    return {"status": "queued", "command_id": cmd.id}


# === OAuth relay for grabette devices ========================================
@app.get("/oauth/grabette/callback", response_model=None)
async def grabette_oauth_relay(
    code: str | None = None,
    state: str | None = None,
    error: str | None = None,
    error_description: str | None = None,
) -> RedirectResponse | HTMLResponse:
    """Relay the HF OAuth callback to the originating grabette on the local network.

    The grabette encodes its mDNS hostname into the OAuth state as
    ``{hostname}|{session_id}``. This endpoint splits that apart and issues a
    302 redirect so the user's browser (on the same LAN as the grabette) reaches
    ``http://{hostname}.local:8000/api/hf-auth/oauth/callback``.

    Only this Space URL needs to be registered as a redirect_uri in the HF
    OAuth app — one entry covers every grabette regardless of hostname.
    """
    if not state or "|" not in state:
        return HTMLResponse("Missing or invalid state parameter.", status_code=400)

    hostname, session_id = state.split("|", 1)
    from urllib.parse import urlencode
    base = f"http://{hostname}.local:8000/api/hf-auth/oauth/callback"

    if error:
        # Forward the error to the grabette so it can mark the session as failed
        # and stop the polling loop on the frontend.
        params: dict = {"error": error, "state": session_id}
        if error_description:
            params["error_description"] = error_description
        return RedirectResponse(f"{base}?{urlencode(params)}", status_code=302)

    if not code:
        return HTMLResponse("Missing code.", status_code=400)

    return RedirectResponse(
        f"{base}?{urlencode({'code': code, 'state': session_id})}",
        status_code=302,
    )


# === UI ======================================================================
@app.get("/")
async def index() -> HTMLResponse:
    return HTMLResponse(_INDEX)


_INDEX = """<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Grabette fleet</title><style>
 *{box-sizing:border-box}
 body{font-family:-apple-system,system-ui,sans-serif;background:linear-gradient(135deg,#1a1a2e,#16213e);
  color:#fff;min-height:100vh;margin:0;padding:2rem;display:flex;justify-content:center}
 .wrap{width:100%;max-width:640px}h1{font-size:1.3rem;margin:0 0 .3rem}
 h2{font-size:1rem;margin:0;display:flex;align-items:center;gap:.5rem}
 .intro{color:#c3cbe0;font-size:.88rem;line-height:1.5;margin:0 0 1.4rem}
 .card{background:rgba(255,255,255,.06);padding:1.2rem;border-radius:14px;margin-bottom:1rem}
 .card.groups{background:linear-gradient(135deg,rgba(139,92,246,.18),rgba(59,130,246,.10));
  border:1px solid rgba(167,139,250,.4);margin-bottom:1.4rem}
 .card.groups h2{color:#c4b5fd}
 .card.groups .count{background:rgba(167,139,250,.22);color:#e9d5ff}
 .card-head{display:flex;align-items:center;justify-content:space-between;margin-bottom:.9rem}
 .count{background:rgba(255,255,255,.12);color:#e2e8f0;font-size:.72rem;font-weight:600;
  padding:.12rem .55rem;border-radius:999px}
 a.btn,button{padding:.55rem 1rem;border:0;border-radius:8px;cursor:pointer;font-weight:600;text-decoration:none;display:inline-block}
 .primary{background:#ffcc4d;color:#1a1a2e}.logout{background:#ef4444;color:#fff}
 .who-row{display:flex;align-items:center;justify-content:space-between;gap:1rem;flex-wrap:wrap}
 .logout-icon{background:#16213e;color:#fff;border:1px solid #2d4a7a;padding:.3rem .5rem;line-height:1}
 .del-icon{background:#16213e;color:#ef4444;border:1px solid #ef4444;padding:.3rem .5rem;line-height:1}
 .rec-icon{background:#16213e;color:#10b981;border:1px solid #2d4a7a;padding:.3rem .5rem;line-height:1}
 .stop-icon{background:#16213e;color:#ef4444;border:1px solid #2d4a7a;padding:.3rem .5rem;line-height:1}
 .dash-icon{background:#16213e;color:#7dd3fc;border:1px solid #2d4a7a;padding:.3rem .5rem;line-height:1}
 button:disabled{opacity:.4;cursor:not-allowed}
 .muted{color:#a0aec0;font-size:.82rem}
 table{width:100%;border-collapse:collapse;font-size:.85rem;table-layout:fixed}
 td,th{text-align:left;padding:.45rem .35rem;border-bottom:1px solid #334;vertical-align:top}
 .c-dot{width:1.2rem}.c-act{width:30%}.c-tools{width:6.6rem;text-align:right}
 .name b{word-break:break-word}
 .result{font-size:.8rem;color:#cbd5e0;
  background:rgba(0,0,0,.25);border-radius:6px;padding:.5rem .6rem;margin-top:.4rem}
 .result.empty{background:none;padding:0}
 .rlabel{font-size:.74rem;font-weight:600;color:#8b98ad;margin-bottom:.35rem;display:flex;align-items:center;gap:.5rem}
 .rclose{margin-left:auto;background:none;border:0;color:#8b98ad;font-size:1.05rem;line-height:1;cursor:pointer;padding:0 .15rem;font-weight:400}
 .rclose:hover{color:#fff}
 .kv{display:grid;grid-template-columns:auto 1fr;gap:.2rem .7rem}
 .kv .k{color:#8b98ad}.kv .v{overflow-wrap:anywhere;word-break:break-word}
 .pill{display:inline-block;padding:.03rem .5rem;border-radius:999px;font-size:.72rem;font-weight:600}
 .pill.rec{background:#ef4444;color:#fff}.pill.idle{background:rgba(255,255,255,.14);color:#cbd5e0}
 .pill.init{background:rgba(245,158,11,.22);color:#fcd34d}
 .pill.stopping{background:rgba(239,68,68,.22);color:#fca5a5}
 .pill.hand{background:rgba(125,211,252,.16);color:#7dd3fc;margin-left:.4rem;vertical-align:middle}
 .rec-dur{font-variant-numeric:tabular-nums;margin-left:.15rem}
 .batt{display:inline-block;margin-left:.4rem;padding:.03rem .45rem;border-radius:999px;font-size:.7rem;font-weight:700;vertical-align:middle}
 .batt::before{content:"🔋 "}
 .batt.batt-ok{background:rgba(16,185,129,.2);color:#a7f3d0}
 .batt.batt-mid{background:rgba(234,179,8,.2);color:#fde68a}
 .batt.batt-low{background:rgba(239,68,68,.22);color:#fca5a5}
 /* device activity badge (capturing / uploading / converting) */
 .act-badge{display:inline-block;margin-left:.4rem;padding:.03rem .5rem;border-radius:999px;font-size:.7rem;font-weight:700;vertical-align:middle;white-space:nowrap}
 .act-badge.act-cap{background:rgba(239,68,68,.22);color:#fca5a5}
 .act-badge.act-up{background:rgba(59,130,246,.2);color:#93c5fd}
 .act-badge.act-proc{background:rgba(139,92,246,.22);color:#d6c9fb}
 .err{color:#fca5a5}
 .raw-d{margin-top:.45rem}.raw-d summary{cursor:pointer;color:#8b98ad;font-size:.72rem}
 .raw-d pre{white-space:pre-wrap;overflow-wrap:anywhere;word-break:break-word;margin:.3rem 0 0;font-size:.72rem;
  max-height:14rem;overflow:auto;background:rgba(0,0,0,.25);border-radius:5px;padding:.4rem .5rem}
 .acts{display:flex;flex-wrap:wrap;gap:.3rem}.acts button{padding:.22rem .7rem;font-size:.8rem}
 .acts button.act-on{box-shadow:inset 0 0 0 2px #1a1a2e}
 .tools{display:flex;gap:.3rem;justify-content:flex-end}
 .dot{display:inline-block;width:.6rem;height:.6rem;border-radius:50%;margin-top:.3rem}
 .on{background:#10b981}.off{background:#ef4444}
 fieldset:disabled{opacity:.45}fieldset{border:0;padding:0;margin:0}code{color:#ffcc4d}
 .c-plus{width:2.4rem}
 .plus-btn{width:1.8rem;height:1.8rem;border-radius:50%;padding:0;font-size:1.1rem;font-weight:700;
  line-height:1;display:inline-flex;align-items:center;justify-content:center}
 .plus-btn.blue{background:#3b82f6;color:#fff}
 .plus-btn.green{background:#10b981;color:#fff}
 .plus-btn.grey{background:#3a4358;color:#6b7280;cursor:not-allowed}
 .pill.group{color:#fff}
 .task-row{padding:.5rem .4rem;border-radius:8px;cursor:pointer}
 .task-row:hover{background:rgba(255,255,255,.06)}
 /* Top line: name takes the full width, action buttons pinned right. */
 .task-top{display:flex;align-items:center;gap:.5rem}
 .task-top .tname{font-weight:600;flex:1;min-width:0;overflow-wrap:anywhere}
 .task-acts{display:flex;gap:.3rem;flex-shrink:0;margin-left:auto}
 /* Second line: "Required devices" tags + description wrap below the name. */
 .task-sub{color:#a0aec0;font-size:.78rem;margin-top:.3rem;display:flex;flex-wrap:wrap;align-items:center;gap:.3rem}
 .task-sub .pill.hand{margin:0}
 #task-editor,.task-edit-panel,.task-del-panel{display:flex;flex-direction:column;gap:.6rem;align-items:stretch}
 .subpanel{margin-top:.9rem;padding:.9rem 1rem;border-radius:10px;border:1px solid rgba(16,185,129,.35);
  background:rgba(0,0,0,.18)}
 .subpanel-title{font-weight:600;font-size:.85rem;color:#6ee7b7}
 .subpanel.task-del-panel{border-color:rgba(239,68,68,.4)}
 .subpanel-title.danger{color:#fca5a5}
 .del-warn{font-size:.85rem;color:#c3cbe0}
 button.del-confirm{background:#ef4444;color:#fff}
 #orphan-bar{margin-bottom:1.4rem}
 .orphan-acc{border:1px solid rgba(245,158,11,.4);border-radius:10px;background:rgba(245,158,11,.08)}
 .orphan-acc-head{display:flex;justify-content:space-between;align-items:center;gap:.5rem;
  padding:.65rem .9rem;cursor:pointer;color:#fcd34d;font-weight:600;font-size:.9rem;user-select:none}
 .orphan-acc-head:hover{background:rgba(245,158,11,.06)}
 .orphan-caret{color:#fcd34d}
 .orphan-acc-body{padding:0 .8rem .8rem;display:flex;flex-direction:column;gap:.7rem}
 .orphan-item{padding:.8rem .9rem;border-radius:10px;border:1px solid rgba(245,158,11,.4);
  background:rgba(245,158,11,.1);display:flex;flex-direction:column;gap:.4rem;align-items:flex-start}
 .orphan-head{font-size:.92rem;color:#fcd34d}
 .orphan-detail{font-size:.82rem;color:#c3cbe0}
 .warn-ico{width:15px;height:15px;flex-shrink:0}
 .orphan-acc-head .warn-ico,.hist-toggle.warn .warn-ico{width:14px;height:14px;vertical-align:-2px}
 .task-warn{display:inline-flex;align-items:center;margin-left:.4rem;color:#fbbf24;vertical-align:middle}
 .task-warn .warn-ico{width:15px;height:15px}
 .hist-toggle.warn{color:#fcd34d}
 .hist-toggle.warn:hover{color:#fde68a}
 select.role-select{width:100%;padding:.5rem .6rem;border-radius:8px;border:1px solid #334;
  background:#fff;color:#111;font:inherit}
 select.role-select option{background:#fff;color:#111}
 .role-pick{display:flex;align-items:center;gap:.6rem}
 .role-pick .sig-label{min-width:5.5rem}
 .fleet-group{margin-top:1rem}.fleet-group:first-of-type{margin-top:.3rem}
 .fleet-group h3{font-size:.9rem;margin:0 0 .4rem;color:#c3cbe0;display:flex;align-items:center;gap:.5rem}
 .subcount{background:rgba(255,255,255,.12);color:#e2e8f0;font-size:.7rem;font-weight:600;padding:.1rem .5rem;border-radius:999px}
 .session-panel{margin-top:.9rem;padding:1rem;border-radius:12px;border:1px solid rgba(239,68,68,.4);
  background:rgba(0,0,0,.22)}
 .sp-head{display:flex;align-items:center;justify-content:space-between;gap:.6rem;margin-bottom:.4rem}
 .sp-task{font-weight:700;font-size:1.05rem}
 /* Big recording timer, centered just above the record button. */
 .sp-timer-wrap{display:flex;justify-content:center;min-height:1.9rem;margin:.6rem 0 .2rem}
 .sp-timer{font-variant-numeric:tabular-nums;font-size:1.9rem;font-weight:800;line-height:1;color:#fff}
 @keyframes sp-pulse{50%{opacity:.35}}
 /* Phone-camera-style record toggle: red circle (start) ↔ red square (stop). */
 .sp-rec{display:flex;justify-content:center;margin:0 0 1rem}
 .rec-toggle{width:72px;height:72px;border-radius:999px;border:4px solid rgba(255,255,255,.55);background:transparent;display:inline-flex;align-items:center;justify-content:center;cursor:pointer;padding:0}
 .rec-toggle:hover{border-color:rgba(255,255,255,.85)}
 .rec-toggle .rt-inner{background:#ef4444;transition:width .15s,height .15s,border-radius .15s}
 .rec-toggle.idle .rt-inner{width:52px;height:52px;border-radius:999px}
 .rec-toggle.recording .rt-inner{width:28px;height:28px;border-radius:8px}
 .rec-toggle.busy{cursor:default;border-color:rgba(255,255,255,.35)}
 .rec-toggle.busy .rt-inner{width:34px;height:34px;border-radius:999px;opacity:.6;animation:sp-pulse 1s infinite}
 /* record circle blocked because a member is busy with a dataset job */
 .rec-toggle.blocked{cursor:not-allowed;opacity:.4;border-color:rgba(255,255,255,.3)}
 .sp-busy-note{margin-top:.6rem;text-align:center;font-size:.8rem;color:#d6c9fb}
 .sp-meta{display:flex;align-items:baseline;gap:.6rem;margin-top:.35rem;font-size:.9rem}
 .sp-label{color:#8b98ad;font-size:.78rem;font-weight:600;min-width:5rem}
 .sp-devs{display:flex;flex-wrap:wrap;gap:.8rem}
 .sp-dev{display:inline-flex;align-items:center;gap:.35rem}
 .sp-actions{display:flex;gap:.5rem;margin-top:.8rem;flex-wrap:wrap}
 .sp-actions button{display:inline-flex;align-items:center;gap:.4rem;padding:.4rem .8rem;font-size:.85rem}
 .sp-actions .del-ep{background:#16213e;color:#ef4444;border:1px solid #ef4444}
 .sp-actions .del-ep:disabled{opacity:.45;cursor:not-allowed}
 .sp-close{display:block;width:100%;margin-top:1rem;padding:.6rem;font-size:.9rem;border:0;border-radius:8px;cursor:pointer;font-weight:600}
 /* ===== Session panel — two zones: recording box vs management box ===== */
 .del-ep{background:#16213e;color:#ef4444;border:1px solid #ef4444;padding:.45rem .8rem;border-radius:8px;font-size:.85rem;cursor:pointer}
 .del-ep:disabled{opacity:.45;cursor:not-allowed}
 .close-btn{background:#ffcc4d;color:#1a1a2e;border:0;padding:.55rem;border-radius:8px;font-weight:700;font-size:.9rem;cursor:pointer;width:100%;margin-top:.8rem}
 .var-c{padding:0;overflow:hidden}
 .sp-rec-zone{padding:1rem;background:rgba(239,68,68,.08)}
 .sp-manage-zone{padding:1rem;background:rgba(255,255,255,.04);border-top:1px solid rgba(255,255,255,.12)}
 /* Delete last episode: bottom-right, inside the recording zone */
 .sp-rec-del{display:flex;justify-content:flex-end;margin-top:.8rem}
 /* Recorded-episode count: one large number */
 .sp-section-label{font-size:.72rem;font-weight:700;color:#8b98ad;letter-spacing:.05em;text-transform:uppercase;margin:.2rem 0 .3rem}
 .ep-big{display:flex;align-items:baseline;gap:.5rem;margin:.15rem 0 .6rem}
 .ep-big-num{font-size:2.1rem;font-weight:800;line-height:1;font-variant-numeric:tabular-nums;
  background:linear-gradient(135deg,#10b981,#3b82f6);-webkit-background-clip:text;background-clip:text;color:transparent}
 .ep-big-lbl{font-size:.82rem;color:#8b98ad}
 .task-block{border-bottom:1px solid #223;}
 .task-detail{padding:.2rem .4rem .7rem 1.6rem}
 .task-launch{display:flex;flex-direction:column;gap:.5rem;margin:.2rem 0 .6rem}
 .launch-hint{font-size:.78rem;align-self:center}
 .section-note{font-size:.8rem;line-height:1.35;margin:.1rem 0 .8rem}
 .task-eps{margin-top:.35rem}
 .ep-count{display:inline-block;font-size:.72rem;color:#a7f3d0;background:rgba(16,185,129,.18);padding:.08rem .55rem;border-radius:999px;white-space:nowrap}
 .ghost{background:rgba(255,255,255,.08);color:#c3cbe0;border:1px solid rgba(255,255,255,.18);padding:.3rem .7rem;font-size:.8rem;border-radius:8px;cursor:pointer}
 .ghost:hover{background:rgba(255,255,255,.14)}
 .task-row.select{cursor:pointer}
 .task-row.select .task-top{gap:.6rem}
 .task-row.disabled{opacity:.4;cursor:not-allowed}
 .task-check{width:16px;height:16px;accent-color:#10b981;pointer-events:none;flex-shrink:0}
 /* Vertical stack: content recap, then the labelled name field, then Private,
    then the button — each on its own line. */
 #dataset-bar{display:none;flex-direction:column;align-items:flex-start;gap:.6rem;
  margin-top:.8rem;padding:.7rem .8rem;border-radius:10px;background:linear-gradient(135deg,rgba(139,92,246,.14),rgba(59,130,246,.10));border:1px solid rgba(139,92,246,.32)}
 #top-sticky{position:sticky;top:0;z-index:30;background:#181e36;padding:.7rem 0;margin-bottom:.9rem;
  display:flex;flex-direction:column;gap:.7rem;box-shadow:0 6px 12px -8px rgba(0,0,0,.7)}
 #top-sticky .card{margin-bottom:0}
 .fleet-head{cursor:pointer;user-select:none;margin-bottom:0}
 .fleet-head h2{display:inline}
 .fleet-caret{color:#8b98ad;font-size:.9rem}
 .fleet-recap{display:inline-flex;flex-wrap:wrap;gap:.3rem;align-items:center}
 .fleet-chip{font-size:.68rem;font-weight:600;color:#c3cbe0;background:rgba(255,255,255,.1);padding:.06rem .45rem;border-radius:999px;white-space:nowrap}
 .fleet-chip.zero{color:#6b7280;background:rgba(255,255,255,.05)}
 .fleet-chip.busy{color:#d6c9fb;background:rgba(139,92,246,.22)}
 #fleet-body{margin-top:.9rem;max-height:42vh;overflow:auto}
 .page-tabs{display:flex;gap:4px;padding:4px;border-radius:999px;
  background:rgba(255,255,255,.05);border:1px solid rgba(255,255,255,.1)}
 .page-tabs .seg-btn{flex:1;justify-content:center}
 .seg-btn{display:inline-flex;align-items:center;justify-content:center;gap:.5rem;border:0;background:transparent;
  color:#aeb8cc;font:inherit;font-size:1rem;font-weight:700;padding:.7rem 1.1rem;border-radius:999px;cursor:pointer;
  white-space:nowrap;transition:background .12s,color .12s,box-shadow .12s}
 .seg-btn svg{width:16px;height:16px}
 .seg-btn:hover{color:#e2e8f0}
 #seg-record.active{background:linear-gradient(135deg,rgba(16,185,129,.5),rgba(59,130,246,.42));color:#fff;box-shadow:0 1px 8px rgba(16,185,129,.3)}
 #seg-dataset.active{background:linear-gradient(135deg,rgba(139,92,246,.5),rgba(59,130,246,.42));color:#fff;box-shadow:0 1px 8px rgba(139,92,246,.3)}
 .mode-dataset .step-num{background:linear-gradient(135deg,#8b5cf6,#3b82f6);color:#fff}
 .mode-dataset .ep-count{background:rgba(139,92,246,.2);color:#d6c9fb}
 #ds-gen{background:linear-gradient(135deg,#8b5cf6,#3b82f6);color:#fff}
 .ds-info{font-size:.82rem;color:#c3cbe0}
 .ds-step{display:flex;align-items:center;gap:.55rem;font-weight:700;font-size:1.05rem;color:#e2e8f0}
 #ds-step1{margin:.2rem 0 .6rem}
 #ds-advanced{margin:-.2rem 0 .7rem}
 .adv-toggle{font-size:.8rem;color:#8b98ad;cursor:pointer;user-select:none;padding:.2rem 0}
 .adv-toggle:hover{color:#c3cbe0}
 .adv-body{display:flex;align-items:center;flex-wrap:wrap;gap:.4rem;padding:.3rem 0 .1rem;font-size:.82rem}
 .use-only{border:1px solid rgba(255,255,255,.18);background:rgba(255,255,255,.05);color:#c3cbe0;
  font:inherit;font-size:.8rem;font-weight:600;padding:.22rem .6rem;border-radius:7px;cursor:pointer}
 .use-only:hover{background:rgba(255,255,255,.12)}
 .use-only.active{background:rgba(16,185,129,.22);color:#a7f3d0;border-color:rgba(16,185,129,.5)}
 .adv-hint{flex-basis:100%;font-size:.75rem;margin-top:.15rem}
 .step-num{display:inline-flex;align-items:center;justify-content:center;width:1.7rem;height:1.7rem;flex-shrink:0;
  border-radius:999px;background:linear-gradient(135deg,#10b981,#3b82f6);color:#fff;font-size:.85rem;font-weight:700}
 .ds-field{display:flex;flex-direction:column;gap:.3rem;width:100%}
 .ds-label{font-size:.75rem;font-weight:600;color:#8b98ad}
 .ds-req{color:#fca5a5}
 .ds-devlabel{font-size:.75rem}
 .ds-devlist{display:flex;flex-wrap:wrap;gap:.3rem .8rem;margin-top:.2rem}
 .ds-dev{display:inline-flex;align-items:center;gap:.35rem;font-size:.82rem;color:#c3cbe0}
 .ds-dot{width:.55rem;height:.55rem;border-radius:999px;flex-shrink:0}
 .ds-dot.on{background:#10b981}
 .ds-dot.off{background:#ef4444}
 .ds-avail{display:inline-flex;align-items:center;gap:.3rem;font-size:.8rem;color:#c3cbe0;cursor:pointer}
 .ds-avail input{width:auto;min-width:0;margin:0}
 .ds-target{display:flex;align-items:center;gap:.3rem;flex-wrap:wrap}
 .ds-target select,.ds-target #ds-name{padding:.35rem .5rem;border-radius:8px;border:1px solid #334;font:inherit;font-size:.82rem}
 .ds-target select{background:#fff;color:#111}
 .ds-target #ds-name{background:rgba(255,255,255,.05);color:#fff;min-width:8rem}
 .ds-slash{color:#8b98ad}
 /* Keep the checkbox tight to its label (don't inherit the text-input width). */
 .ds-private{display:inline-flex;align-items:center;gap:.25rem;font-size:.8rem;color:#c3cbe0;cursor:pointer;white-space:nowrap}
 .ds-private input{width:auto;min-width:0;margin:0}
 .ds-job{width:100%;font-size:.8rem;color:#c3cbe0}
 .ds-job.err{color:#fca5a5}
 .ds-job.ok{color:#a7f3d0}
 .ds-progress{width:100%;height:6px;border-radius:999px;background:rgba(255,255,255,.12);overflow:hidden}
 .ds-progress-fill{height:100%;width:0;border-radius:999px;background:#10b981;transition:width .3s ease}
 .ds-progress.indet .ds-progress-fill{width:40%;background:linear-gradient(90deg,rgba(16,185,129,0),#10b981,rgba(16,185,129,0));animation:ds-indet 1.2s linear infinite}
 @keyframes ds-indet{0%{transform:translateX(-120%)}100%{transform:translateX(320%)}}
 .ds-job a{color:#7dd3fc}
 .hist-toggle{font-size:.8rem;color:#8b98ad;cursor:pointer;user-select:none;padding:.25rem 0}
 .hist-toggle:hover{color:#c3cbe0}
 .hist-row{font-size:.82rem;padding:.25rem 0;color:#cbd5e0}
 .hist-head{display:flex;align-items:center;justify-content:space-between;gap:.6rem;flex-wrap:wrap}
 .fill-task{background:#16213e;color:#7dd3fc;border:1px solid #2d4a7a;padding:.2rem .6rem;border-radius:7px;font-size:.76rem;cursor:pointer;white-space:nowrap}
 .fill-ep-hint{font-size:.75rem;color:#8b98ad;margin-left:.3rem;white-space:nowrap}
 .act-edit{background:#16213e;color:#7dd3fc;border:1px solid #2d4a7a;padding:.3rem .5rem;line-height:1}
 #task-name-input,#task-desc-input,.edit-input{width:100%;padding:.5rem .6rem;border-radius:8px;border:1px solid #334;
  background:rgba(255,255,255,.05);color:#fff;font:inherit}
 .sig-row{display:flex;align-items:center;gap:.7rem;flex-wrap:wrap}
 .sig-label{color:#c3cbe0;font-size:.85rem;font-weight:600}
 .sig-label.task-label{font-size:1.05rem;font-weight:700;color:#e2e8f0}
 .sig-checks{display:flex;gap:.8rem;align-items:center;color:#c3cbe0;font-size:.85rem}
 .sig-checks label{display:flex;align-items:center;gap:.3rem;cursor:pointer}
 .editor-actions{display:flex;gap:.5rem}
 .pill.task{background:rgba(255,204,77,.16);color:#ffcc4d}
 .card.tasks{margin-bottom:1.4rem}
 .card.sessions{background:linear-gradient(135deg,rgba(239,68,68,.14),rgba(139,92,246,.10));
  border:1px solid rgba(239,68,68,.32);margin-bottom:1.4rem}
 .card.sessions h2{color:#fca5a5}
 .card.sessions .count{background:rgba(239,68,68,.22);color:#fecaca}
 .pill.open{background:#10b981;color:#fff}
 .pill.closed{background:rgba(255,255,255,.14);color:#cbd5e0}
 button.validate{background:linear-gradient(135deg,#10b981,#3b82f6);color:#fff}
 button.cancel{background:rgba(255,255,255,.12);color:#e2e8f0}
 /* Phone: reclaim the wide desktop padding and let side-by-side rows stack. */
 @media (max-width:600px){
  body{padding:.7rem}
  .card{padding:.9rem}
  .role-pick{flex-wrap:wrap}
  .role-pick select{flex:1;min-width:0}
  .sig-row{align-items:flex-start}
 }
</style></head><body><div class="wrap">
<h1>Grabette fleet <span class="muted">— operator dashboard</span></h1>
<p class="intro">This dashboard lists every Grabette, Gripette and Casquette that has been
 detected and is connected to your HuggingFace account. Each device is grouped by type below;
 sign in with HuggingFace to see and control the devices that report to your fleet.</p>
<div class="card"><h2>HuggingFace login</h2><div id="who" style="margin-top:.7rem">Checking…</div></div>
<fieldset id="gated" disabled>
 <div id="top-sticky">
  <div class="page-tabs" id="page-tabs" role="tablist" aria-label="Mode">
   <button class="seg-btn active" id="seg-record" role="tab" onclick="setSelectMode(false)"><svg viewBox="0 0 24 24" fill="currentColor"><circle cx="12" cy="12" r="7"/></svg>Record</button>
   <button class="seg-btn" id="seg-dataset" role="tab" onclick="setSelectMode(true)"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 2 2 7l10 5 10-5-10-5Z"/><path d="m2 17 10 5 10-5"/><path d="m2 12 10 5 10-5"/></svg>Build dataset</button>
  </div>
  <div class="card fleet-acc" id="fleet-card">
   <div class="card-head fleet-head" onclick="toggleFleet()">
    <div style="display:flex;align-items:center;gap:.5rem;flex-wrap:wrap"><h2>Fleet</h2><span class="fleet-recap" id="fleet-recap"></span></div>
    <span class="fleet-caret" id="fleet-caret">▸</span>
   </div>
   <div id="fleet-body" style="display:none">
    <div class="fleet-group" data-kind="grabette">
     <h3>Grabettes <span class="subcount" id="count-grabette">0</span></h3>
     <table><tbody id="tb-grabette"></tbody></table>
     <div class="muted empty" id="empty-grabette">No Grabettes detected yet.</div>
    </div>
    <div class="fleet-group" data-kind="gripette">
     <h3>Gripettes <span class="subcount" id="count-gripette">0</span></h3>
     <table><tbody id="tb-gripette"></tbody></table>
     <div class="muted empty" id="empty-gripette">No Gripettes detected yet.</div>
    </div>
    <div class="fleet-group" data-kind="casquette">
     <h3>Casquettes <span class="subcount" id="count-casquette">0</span></h3>
     <table><tbody id="tb-casquette"></tbody></table>
     <div class="muted empty" id="empty-casquette">No Casquettes detected yet.</div>
    </div>
   </div>
  </div>
 </div>
 <div id="orphan-bar" style="display:none"></div>
 <div class="card sessions" id="sessions-card" style="display:none">
  <div class="card-head"><div style="display:flex;align-items:center;gap:.5rem"><h2><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>Session</h2></div></div>
  <div id="session-list"></div>
 </div>
 <div class="card tasks mode-record" id="tasks-card">
  <div class="card-head"><div style="display:flex;align-items:center;gap:.5rem"><h2><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 11l3 3L22 4"/><path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"/></svg>Tasks</h2><span class="count" id="count-tasks">0</span></div></div>
  <p class="section-note muted">These tasks come from the grabettes connected to this account — those online now, plus any connected earlier in this session. Episode counts reflect only the grabettes currently online.</p>
  <div id="ds-step1" class="ds-step" style="display:none"></div>
  <div id="ds-advanced" style="display:none"></div>
  <div id="task-list" class="muted empty">No tasks yet.</div>
  <div id="dataset-bar"></div>
  <button class="primary" id="btn-create-task" onclick="startCreateTask()" style="margin-top:.9rem">Create task</button>
  <div id="task-editor" class="subpanel" style="display:none">
   <div class="subpanel-title" id="task-editor-title">New task</div>
   <input id="task-name-input" type="text" placeholder="Task name">
   <input id="task-desc-input" type="text" placeholder="Description (optional)">
   <div class="sig-row">
    <span class="sig-label">Required devices</span>
    <span class="sig-checks">
     <label><input type="checkbox" id="task-sig-left">lgrabette</label>
     <label><input type="checkbox" id="task-sig-right">rgrabette</label>
     <label><input type="checkbox" id="task-sig-casquette">casquette</label>
    </span>
   </div>
   <div class="editor-actions">
    <button class="validate" onclick="validateTask()">Validate task</button>
    <button class="cancel" onclick="cancelTaskEdit()">Cancel</button>
   </div>
  </div>
 </div>
</fieldset></div><script>
const $=id=>document.getElementById(id);let loggedIn=false;
const KINDS=['grabette','gripette','casquette'];
let DEVICES=[];let TASKS=[];let SESSIONS=[];let ORPHANS=[];
let taskEditId=null;let taskDeleteId='';
let launchRolePick={left:'',right:'',casquette:''};
let expandedTaskId='';let historyOpenId='';let issuesOpenId='';let orphansOpen=false;let groupsOpenId='';let fleetOpen=false;
// Dataset selection mode: pick several tasks (same device signature) to build a
// LeRobot dataset. The first pick locks the signature; incompatible tasks are
// disabled. Selection persists across the 3s refresh (module-level state).
let selectMode=false;let datasetSel=new Set();let datasetJob=null;let datasetPollTimer=null;
let datasetName='';let datasetOwner='';let datasetNamespaces=null;let datasetPrivate=false;let datasetOnlyAvailable=false;
// "Use only" advanced option: a role subset (empty = each task's full signature).
let datasetUseOnly=[];let datasetAdvOpen=false;
const USE_ONLY_OPTS=[{label:'L',roles:['left']},{label:'R',roles:['right']},{label:'LC',roles:['left','casquette']},{label:'RC',roles:['right','casquette']}];
const ROLE_LABEL={left:'lgrabette',right:'rgrabette',casquette:'casquette'};
function deviceName(device_id){const d=DEVICES.find(x=>x.device_id===device_id);return d?d.name:device_id;}
function deviceBattery(device_id){const d=DEVICES.find(x=>x.device_id===device_id);return d?d.battery:null;}
function batteryPill(b){
 if(b===null||b===undefined)return '';
 const p=Math.round(b);const cls=p<=15?'batt-low':p<=40?'batt-mid':'batt-ok';
 return `<span class="batt ${cls}" title="Battery ${p}%">${p}%</span>`;}
function fmtDur(ms){const s=Math.max(0,Math.floor(ms/1000));return Math.floor(s/60)+':'+String(s%60).padStart(2,'0');}
function tickRecDur(){document.querySelectorAll('.rec-dur').forEach(el=>{const st=+el.dataset.start;if(st)el.textContent=fmtDur(Date.now()-st);});}
function slotOf(d){const k=kindOf(d);if(k==='casquette')return 'casquette';if(k==='grabette'&&(d.hand==='left'||d.hand==='right'))return d.hand;return null;}
// device activity ('idle'|'capturing'|'uploading'|'processing') reported by
// the fleet (self-reported by the device, or inferred from dispatched work).
function deviceActivity(id){const d=DEVICES.find(x=>x.device_id===id);return (d&&d.activity)||'idle';}
// Busy with dataset work → cannot (re)start a recording (mirrors the server gate).
function deviceBusyForRec(id){const a=deviceActivity(id);return a==='uploading'||a==='processing';}
const ACT_LABEL={capturing:'● Recording',uploading:'↑ Uploading',processing:'⚙ Converting'};
function activityBadge(id){
 const a=deviceActivity(id);
 if(a==='idle')return '';
 const cls={capturing:'act-cap',uploading:'act-up',processing:'act-proc'}[a]||'';
 return `<span class="act-badge ${cls}" title="Device is ${a}">${ACT_LABEL[a]||a}</span>`;}
function errText(j){const d=j&&j.detail;if(!d)return '';if(typeof d==='string')return d;return d.message||JSON.stringify(d);}
// ── Tasks ──
function startCreateTask(){taskEditId=null;$('task-editor-title').textContent='New task';$('task-name-input').value='';$('task-desc-input').value='';
 ['left','right','casquette'].forEach(s=>$('task-sig-'+s).checked=false);
 $('task-editor').style.display='flex';$('btn-create-task').style.display='none';}
// Edit happens INLINE, in the task's own block (see taskEditorHtml in
// renderTaskList) — not in the bottom editor — so it's visible even far down a
// long list. Just flag the task and re-render.
function editTask(id){taskEditId=(taskEditId===id)?'':id;expandedTaskId='';renderTaskList();}
// Inline editor for one task. The required devices are LOCKED once they're KNOWN
// and the task has episodes (changing them would misdescribe the recordings). An
// old task with episodes but no recorded signature can be set once to backfill it.
function sigLocked(t){return (t.episode_count||0)>0 && (t.device_signature||[]).length>0;}
function taskEditorHtml(t){
 const locked=sigLocked(t);
 const backfill=(t.episode_count||0)>0 && !locked;  // old task: set the devices once
 const chk=s=>`<label><input type="checkbox" class="edit-sig" value="${s}" ${(t.device_signature||[]).includes(s)?'checked':''} ${locked?'disabled':''}>${ROLE_LABEL[s]}</label>`;
 let note='';
 if(locked)note=`<div class="muted" style="font-size:.8rem">Locked — this task already has ${t.episode_count} recorded episode(s), so its required devices can't change.</div>`;
 else if(backfill)note=`<div class="muted" style="font-size:.8rem">This task has recordings but no devices set — choose them now to fix it. This can only be set once.</div>`;
 return `<div class="subpanel task-edit-panel">
   <div class="subpanel-title">Edit task</div>
   <input id="edit-name" class="edit-input" type="text" placeholder="Task name" value="${esc(t.name)}">
   <input id="edit-desc" class="edit-input" type="text" placeholder="Description (optional)" value="${esc(t.description||'')}">
   <div class="sig-row"><span class="sig-label">Required devices</span>
    <span class="sig-checks">${chk('left')}${chk('right')}${chk('casquette')}</span></div>
   ${note}
   <div class="editor-actions">
    <button class="validate" onclick="validateTask()">Save</button>
    <button class="cancel" onclick="editTask('${t.id}')">Cancel</button>
   </div>
  </div>`;}
function cancelTaskEdit(){taskEditId=null;$('task-editor').style.display='none';$('btn-create-task').style.display='inline-block';}
async function validateTask(){
 let name,description,device_signature;
 if(taskEditId){  // inline editor
  name=$('edit-name').value.trim();description=$('edit-desc').value.trim();
  device_signature=[...document.querySelectorAll('.edit-sig:checked')].map(c=>c.value);
 }else{           // bottom "create task" editor
  name=$('task-name-input').value.trim();description=$('task-desc-input').value.trim();
  device_signature=['left','right','casquette'].filter(s=>$('task-sig-'+s).checked);
 }
 if(!name){alert('Task needs a name');return;}
 if(!device_signature.length){alert('Select at least one required device');return;}
 const body={name,description,device_signature};
 const url=taskEditId?`/api/fleet/tasks/${taskEditId}`:'/api/fleet/tasks';
 const method=taskEditId?'PUT':'POST';
 const r=await fetch(url,{method,headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});
 if(!r.ok){const j=await r.json().catch(()=>({}));alert(errText(j)||'Failed to save task');return;}
 taskEditId=null;$('task-editor').style.display='none';$('btn-create-task').style.display='inline-block';
 await refresh();}
// Delete asks for confirmation in an inline panel (taskDeleteHtml) that spells
// out the consequences (episodes + which devices lose their recordings), rather
// than a bare confirm(). Clicking delete again cancels.
function deleteTask(id){taskDeleteId=(taskDeleteId===id)?'':id;taskEditId='';expandedTaskId='';renderTaskList();}
function taskDeleteHtml(t){
 const eps=t.episodes||[];
 const devs=new Map();  // device_id -> {name, online} across all this task's episodes
 for(const ep of eps)for(const m of Object.values(ep.members||{}))if(m&&m.device_id)devs.set(m.device_id,{name:m.name,online:deviceOnline(m.device_id)});
 const list=[...devs.values()].map(d=>`<span class="ds-dev"><span class="ds-dot ${d.online?'on':'off'}"></span>${esc(d.name)}${d.online?'':' <span class="muted">offline</span>'}</span>`).join('');
 const offline=[...devs.values()].filter(d=>!d.online).length;
 return `<div class="subpanel task-del-panel">
   <div class="subpanel-title danger">Delete “${esc(t.name)}”?</div>
   <div class="del-warn">This permanently deletes the task and its <b>${eps.length}</b> recorded episode(s) on:</div>
   <div class="ds-devlist">${list||'<span class="muted">no recording devices</span>'}</div>
   ${offline?`<div class="del-warn muted">${offline} device(s) offline — they keep their copy until they reconnect, then you'll be prompted to finish the cleanup.</div>`:''}
   <div class="editor-actions">
    <button class="del-confirm" onclick='confirmDeleteTask("${t.id}")'>Delete permanently</button>
    <button class="cancel" onclick='deleteTask("${t.id}")'>Cancel</button>
   </div>
  </div>`;}
async function confirmDeleteTask(id){
 const r=await fetch(`/api/fleet/tasks/${id}`,{method:'DELETE'});
 if(!r.ok){const j=await r.json().catch(()=>({}));alert(errText(j)||'Failed to delete task');return;}
 taskDeleteId='';await refresh();}
function sigBadges(sig){return (sig||[]).map(s=>`<span class="pill hand" title="${s} role">${s[0].toUpperCase()}</span>`).join(' ');}
function warnIcon(){return '<svg class="warn-ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>';}
function toggleIssues(id){issuesOpenId=issuesOpenId===id?'':id;renderTaskList();}
function toggleGroups(id){groupsOpenId=groupsOpenId===id?'':id;renderTaskList();}
function toggleFleet(){fleetOpen=!fleetOpen;$('fleet-body').style.display=fleetOpen?'':'none';$('fleet-caret').textContent=fleetOpen?'▾':'▸';}
// Header recap (accordion collapsed): count of each device type ONLINE now.
function renderFleetRecap(){
 let lg=0,rg=0,grip=0,casq=0,busy=0;
 for(const d of DEVICES){
  if(!d.online)continue;
  if(d.activity==='uploading'||d.activity==='processing')busy++;
  const k=kindOf(d);
  if(k==='grabette'){if(d.hand==='left')lg++;else if(d.hand==='right')rg++;}
  else if(k==='gripette')grip++;
  else if(k==='casquette')casq++;
 }
 const chip=(label,n,title)=>`<span class="fleet-chip${n?'':' zero'}" title="${title}">${label} ${n}</span>`;
 // Busy chip is visible even with the Fleet accordion collapsed (e.g. from Record).
 const busyChip=busy?`<span class="fleet-chip busy" title="${busy} device(s) busy with a dataset upload/conversion">⚙ ${busy} busy</span>`:'';
 $('fleet-recap').innerHTML=chip('L',lg,'lgrabettes online')+chip('R',rg,'rgrabettes online')+chip('C',casq,'casquettes online')+chip('Grip',grip,'gripettes online')+busyChip;}
// Distinct device combinations used to record a task, with episode count each.
// Built from the connected devices' reported episodes (their members map).
function taskCombos(t){
 const m=new Map();
 for(const ep of (t.episodes||[])){
  const entries=Object.entries(ep.members||{}).sort((a,b)=>a[0].localeCompare(b[0]));
  if(!entries.length)continue;
  const key=entries.map(([r,mm])=>r+':'+(mm.device_id||'?')).join('|');
  let g=m.get(key);
  if(!g){g={roles:entries.map(([r,mm])=>({role:r,name:mm.name,online:deviceOnline(mm.device_id)})),count:0};m.set(key,g);}
  g.count++;
 }
 return [...m.values()];}
function comboRowHtml(g){
 const devs=g.roles.map(r=>`<span class="pill hand" title="${r.role}">${r.role[0].toUpperCase()}</span> ${esc(r.name)}${r.online?'':' <span class="muted">offline</span>'}`).join(' · ');
 return `<div class="hist-row">${devs} — <b>${g.count}</b> episode${g.count>1?'s':''}</div>`;}
function toggleTaskDetail(id){
 if(expandedTaskId===id){expandedTaskId='';}
 else{expandedTaskId=id;launchRolePick={left:'',right:'',casquette:''};historyOpenId='';}
 renderTaskList();}
function toggleHistory(id){historyOpenId=historyOpenId===id?'':id;renderTaskList();}
function fmtTs(sec){try{return new Date(sec*1000).toLocaleString();}catch(e){return '';}}
// ── Dataset selection ──
function sigKey(sig){return (sig||[]).slice().sort().join('+');}
function datasetLockedSig(){for(const t of TASKS){if(datasetSel.has(t.id))return sigKey(t.device_signature);}return '';}
function useOnlySet(){return datasetUseOnly.length>0;}
function sameRoles(a,b){return a.length===b.length&&a.every(r=>b.includes(r));}
function taskHasRoles(t,roles){const sig=new Set(t.device_signature||[]);return roles.every(r=>sig.has(r));}
// A task can be picked for the dataset if: (with "use only") its signature is a
// superset of the chosen roles; else (default) it matches the first pick's exact
// signature. This is what lets a bimanual task be used for a left-only dataset.
function datasetCompatible(t){
 if(useOnlySet())return taskHasRoles(t,datasetUseOnly);
 const locked=datasetLockedSig();
 return !locked||sigKey(t.device_signature)===locked;}
// Roles actually used to build the dataset: the "use only" subset, or null=all.
function activeRoles(){return useOnlySet()?datasetUseOnly:null;}
function setUseOnly(i){
 const roles=USE_ONLY_OPTS[i].roles;
 datasetUseOnly=sameRoles(datasetUseOnly,roles)?[]:roles.slice();  // click active = clear
 // Drop any now-incompatible selection under the new constraint.
 for(const id of [...datasetSel]){const t=TASKS.find(x=>x.id===id);if(!t||!datasetCompatible(t))datasetSel.delete(id);}
 renderTaskList();}
function renderAdvanced(){
 const el=$('ds-advanced');
 if(!selectMode||!TASKS.length){el.style.display='none';return;}
 el.style.display='block';
 const opts=USE_ONLY_OPTS.map((o,i)=>`<button class="use-only${sameRoles(datasetUseOnly,o.roles)?' active':''}" onclick="setUseOnly(${i})">${o.label}</button>`).join('');
 el.innerHTML=`<div class="adv-toggle" onclick="datasetAdvOpen=!datasetAdvOpen;renderAdvanced()">${datasetAdvOpen?'▾':'▸'} Advanced options</div>`+
  (datasetAdvOpen?`<div class="adv-body"><span class="muted">Use only:</span> ${opts}`+
   `<div class="adv-hint muted">Includes any task that has at least these devices, and uploads only these to build the dataset.</div></div>`:'');}
// Switch the task list between the two segmented modes. Idempotent: re-clicking
// the active segment is a no-op (so a dataset in progress isn't reset).
function setSelectMode(on){
 on=!!on;
 if(on===selectMode)return;
 selectMode=on;datasetSel.clear();expandedTaskId='';datasetJob=null;
 // Reset any open task editor/delete/create panel so it doesn't linger across modes.
 taskEditId=null;taskDeleteId='';$('task-editor').style.display='none';
 if(datasetPollTimer){clearTimeout(datasetPollTimer);datasetPollTimer=null;}
 if(selectMode){datasetName='';datasetPrivate=false;datasetOnlyAvailable=false;datasetUseOnly=[];datasetAdvOpen=false;if(datasetNamespaces===null)fetchNamespaces();}
 $('seg-record').classList.toggle('active',!selectMode);
 $('seg-dataset').classList.toggle('active',selectMode);
 // Drive the per-mode accent colour of the numbers/counts/encart below.
 $('tasks-card').classList.toggle('mode-dataset',selectMode);
 $('tasks-card').classList.toggle('mode-record',!selectMode);
 $('btn-create-task').style.display=selectMode?'none':'inline-block';
 renderTaskList();}  // renderTaskList sets the step-1 header text for the mode
async function fetchNamespaces(){
 try{
  const r=await fetch('/api/fleet/namespaces');
  if(r.ok){const j=await r.json();datasetNamespaces=j.namespaces||[];
   if(!datasetOwner)datasetOwner=j.default||datasetNamespaces[0]||'';
   renderDatasetBar();}
 }catch(e){}
}
function toggleDatasetPick(id){
 const t=TASKS.find(x=>x.id===id);if(!t)return;
 if(datasetSel.has(id)){datasetSel.delete(id);}
 else{if(!datasetCompatible(t))return;datasetSel.add(id);}
 renderTaskList();}
// ── Dataset device involvement (computed live from the tasks' reported episodes + DEVICES) ──
function deviceOnline(id){const d=DEVICES.find(x=>x.device_id===id);return !!(d&&d.online);}
function datasetEpisodes(){ // every episode across the selected tasks (from device reports)
 const eps=[];
 for(const t of TASKS){if(!datasetSel.has(t.id))continue;for(const ep of (t.episodes||[]))eps.push(ep);}
 return eps;}
function datasetDevices(){ // Map device_id -> role, union over selected episodes (only the active roles)
 const m=new Map();const only=activeRoles();
 for(const ep of datasetEpisodes())for(const [role,dev] of Object.entries(ep.roles||{})){
  if(only&&!only.includes(role))continue;
  m.set(dev,role);}
 return m;}
function datasetEpisodeCounts(){ // total, and how many have ALL their (active-role) devices online
 let total=0,avail=0;const only=activeRoles();
 for(const ep of datasetEpisodes()){
  const d=Object.entries(ep.roles||{}).filter(([r])=>!only||only.includes(r)).map(([,dev])=>dev);
  if(!d.length)continue;
  total++;if(d.every(deviceOnline))avail++;}
 return {total,avail};}
function datasetDevicesHtml(){
 const m=datasetDevices();
 if(!m.size)return '';
 const items=[...m.entries()].map(([id,role])=>{
  const on=deviceOnline(id);
  return `<span class="ds-dev"><span class="ds-dot ${on?'on':'off'}"></span>${esc(deviceName(id))}`+
   ` <span class="pill hand">${role[0].toUpperCase()}</span>${on?'':' <span class="muted">offline</span>'}</span>`;
 }).join('');
 return `<span class="ds-devlabel muted">Devices needed:</span><span class="ds-devlist">${items}</span>`;}
function renderDatasetBar(){
 const bar=$('dataset-bar');
 if(!selectMode){bar.style.display='none';bar.innerHTML='';return;}
 bar.style.display='flex';
 const sel=TASKS.filter(t=>datasetSel.has(t.id));
 const counts=datasetEpisodeCounts();
 const epText=datasetOnlyAvailable?`${counts.avail} / ${counts.total} episodes`:`${counts.total} episodes`;
 // With "use only" the dataset's roles are the chosen subset, not the task sig.
 const sig=useOnlySet()?datasetUseOnly:(sel.length?sel[0].device_signature:[]);
 const infoHtml=`${sel.length} task(s) · ${sigBadges(sig)||'<span class="muted">no device set</span>'} · ${epText}`;
 let jobCls='',jobHtml='';
 if(datasetJob){
  const m=esc(datasetJob.message||datasetJob.status||'');
  if(datasetJob.status==='error'){jobCls='err';jobHtml='✗ '+m;}
  else if(datasetJob.status==='done'){jobCls='ok';jobHtml='✓ '+m+(datasetJob.result_url?` — <a href="${datasetJob.result_url}" target="_blank" rel="noopener">open</a>`:'');}
  else{jobHtml='… '+m;}
 }
 // Build the controls once (so the name <input> keeps focus/value across the 3s
 // refresh re-renders); afterwards only update the dynamic text/state.
 if(!$('ds-name')){
  bar.innerHTML=
   `<div class="ds-step"><span class="step-num">2</span> Name your destination repository</div>`+
   `<span class="ds-info" id="ds-info">${infoHtml}</span>`+
   `<div id="ds-devices"></div>`+
   `<label class="ds-avail"><input type="checkbox" id="ds-avail" ${datasetOnlyAvailable?'checked':''} onchange="datasetOnlyAvailable=this.checked;renderDatasetBar()"> Use only available devices</label>`+
   `<div class="ds-field"><label class="ds-label" for="ds-name">Dataset name <span class="ds-req">*</span></label>`+
   `<span class="ds-target"><select id="ds-owner" onchange="datasetOwner=this.value"></select>`+
   `<span class="ds-slash">/</span>`+
   `<input id="ds-name" placeholder="dataset-name" value="${esc(datasetName)}" oninput="datasetName=this.value;$('ds-gen').disabled=!dsCanGen()"></span></div>`+
   `<label class="ds-private"><input type="checkbox" id="ds-private" ${datasetPrivate?'checked':''} onchange="datasetPrivate=this.checked"> Private</label>`+
   `<button class="validate" id="ds-gen" onclick="generateDataset()">Generate LeRobot dataset</button>`+
   `<div class="ds-progress" id="ds-progress" style="display:none"><div class="ds-progress-fill"></div></div>`+
   `<div class="ds-job" id="ds-job"></div>`;
 }else{
  $('ds-info').innerHTML=infoHtml;
 }
 $('ds-devices').innerHTML=datasetDevicesHtml();
 // Owner options: (re)fill when the namespace list changes; keep the selection.
 const ownerSel=$('ds-owner');
 const opts=(datasetNamespaces&&datasetNamespaces.length)?datasetNamespaces:(datasetOwner?[datasetOwner]:[]);
 if(ownerSel.options.length!==opts.length)
  ownerSel.innerHTML=opts.map(n=>`<option value="${esc(n)}">${esc(n)}</option>`).join('');
 if(datasetOwner)ownerSel.value=datasetOwner;
 $('ds-gen').disabled=!dsCanGen();
 const jobEl=$('ds-job');jobEl.className='ds-job'+(jobCls?' '+jobCls:'');jobEl.innerHTML=jobHtml;
 // Progress bar: determinate during upload (fraction of devices done), animated
 // (indeterminate) during the opaque Space conversion, full & green when done.
 const prog=$('ds-progress'),fill=prog.querySelector('.ds-progress-fill');
 if(!datasetJob||datasetJob.status==='error'){prog.style.display='none';}
 else{
  prog.style.display='block';
  const done=datasetJob.status==='done';
  // Same animated (indeterminate) bar for both work phases (upload + convert);
  // full green only when done. Each phase can take several minutes.
  const indet=!done;
  prog.className='ds-progress'+(indet?' indet':'')+(done?' done':'');
  fill.style.width=indet?'':'100%';
 }}
// Generate requires: ≥1 task, a name, no running job, at least one usable
// episode, and — in default mode — every needed device online (else the backend
// refuses). With "use only available devices", offline devices are tolerated
// (their episodes are dropped) as long as ≥1 episode remains.
function dsCanGen(){
 const busy=datasetJob&&(datasetJob.status==='uploading'||datasetJob.status==='processing');
 if(busy||datasetSel.size===0||!(datasetName||'').trim())return false;
 const c=datasetEpisodeCounts();
 if(datasetOnlyAvailable)return c.avail>0;
 if(c.total===0)return false;
 for(const id of datasetDevices().keys())if(!deviceOnline(id))return false;
 return true;}
async function generateDataset(){
 const ids=[...datasetSel];
 if(!ids.length)return;
 const name=(datasetName||'').trim();
 if(!name){alert('Enter a dataset name');return;}
 const target=datasetOwner?datasetOwner+'/'+name:name;
 const r=await fetch('/api/fleet/lerobot-dataset',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({task_ids:ids,name:target,private:datasetPrivate,only_available:datasetOnlyAvailable,roles:datasetUseOnly})});
 const j=await r.json().catch(()=>({}));
 if(!r.ok){alert(errText(j)||'Failed to start dataset generation');return;}
 datasetJob={status:'uploading',message:'Starting… (this can take several minutes)'};renderDatasetBar();
 pollDatasetJob(j.job_id);}
function pollDatasetJob(jobId){
 if(datasetPollTimer)clearTimeout(datasetPollTimer);
 const step=async()=>{
  try{
   const r=await fetch('/api/fleet/lerobot-dataset/'+jobId);
   if(r.ok){datasetJob=await r.json();renderDatasetBar();
    if(datasetJob.status==='done'||datasetJob.status==='error'||datasetJob.status==='raw_ready')return;}
  }catch(e){}
  datasetPollTimer=setTimeout(step,2000);
 };
 step();}
// Per-role device dropdowns for a task's required devices. Selected values are
// baked in as `selected` attributes so the 3s refresh re-render keeps the picks.
function pickRole(role,val){launchRolePick[role]=val;renderTaskList();}
function launchRolesHtml(t){
 return (t.device_signature||[]).map(role=>{
  const opts=DEVICES.filter(d=>slotOf(d)===role).map(d=>{
   const sel=launchRolePick[role]===d.device_id?' selected':'';
   // Offline devices can't be picked — a session needs every device online.
   // Nor can a device busy with a dataset upload/conversion.
   const busy=deviceBusyForRec(d.device_id);
   const dis=(!d.online||busy)?' disabled':'';
   const suffix=!d.online?' (offline)':busy?` (${deviceActivity(d.device_id)==='processing'?'converting':'uploading'})`:'';
   return `<option value="${d.device_id}"${sel}${dis}>${esc(d.name)}${suffix}</option>`;
  }).join('');
  const none=launchRolePick[role]?'':' selected';
  return `<label class="role-pick"><span class="sig-label">${ROLE_LABEL[role]}</span>`+
   `<select class="role-select" onchange="pickRole('${role}',this.value)">`+
   `<option value=""${none}>— select —</option>${opts}</select></label>`;
 }).join('');}
// Expanded task detail: the session launcher (role dropdowns + Launch button),
// then a collapsed-by-default session history.
function taskDetailHtml(t){
 const running=SESSIONS.some(s=>s.status==='open');
 // Episodes recorded for this task, newest first — sourced from the devices'
 // reports so they show (and name their peers, even offline ones) for ANY
 // operator, regardless of which account did the acquisition.
 const eps=(t.episodes||[]).slice().sort((a,b)=>b.episode_id.localeCompare(a.episode_id));
 const roles=t.device_signature||[];
 const missing=roles.filter(role=>!launchRolePick[role]);
 const offlinePick=roles.filter(role=>launchRolePick[role]&&!deviceOnline(launchRolePick[role]));
 const busyPick=roles.filter(role=>launchRolePick[role]&&deviceBusyForRec(launchRolePick[role]));
 let launchBtn;
 if(running){
  launchBtn=`<button class="validate" disabled>Launch session</button><span class="muted launch-hint">A session is already running — close it first.</span>`;
 }else if(missing.length){
  launchBtn=`<button class="validate" disabled>Launch session</button><span class="muted launch-hint">Select a device for every role — required to launch.</span>`;
 }else if(offlinePick.length){
  launchBtn=`<button class="validate" disabled>Launch session</button><span class="muted launch-hint">A selected device is offline — every device must be online.</span>`;
 }else if(busyPick.length){
  launchBtn=`<button class="validate" disabled>Launch session</button><span class="muted launch-hint">A selected device is busy processing a dataset — wait for it to finish.</span>`;
 }else{
  launchBtn=`<button class="validate" onclick='launchSession("${t.id}")'>Launch session</button>`;
 }
 const histOpen=historyOpenId===t.id;
 const epRows=eps.length?eps.map(ep=>{
  const names=Object.entries(ep.members||{}).map(([role,m])=>`<span class="pill hand" title="${role}">${role[0].toUpperCase()}</span> ${esc(m.name)}${deviceOnline(m.device_id)?'':' <span class="muted">offline</span>'}`).join(' ')||'<span class="muted">no devices recorded</span>';
  // Episodes saved before the device persisted their members show up incomplete;
  // repair is bulk, at the task level (see the "Fill devices" button below).
  const mark=ep.incomplete?` <span class="fill-ep-hint" title="Recorded before members were saved — use “Fill devices” to repair">${warnIcon()} incomplete</span>`:'';
  return `<div class="hist-row"><span class="muted">${fmtTs(ep.started_at)}</span> · ${names}${mark}</div>`;
 }).join(''):'<div class="muted" style="font-size:.82rem">No episode recorded yet.</div>';
 // Task-level repair: count incomplete episodes and how many can be filled now.
 const incN=eps.filter(e=>e.incomplete).length;
 const fillN=eps.filter(e=>e.fillable).length;
 const fillCtrl=fillN
  ?`<button class="fill-task" title="Backfill who recorded these episodes onto the connected grabettes" onclick='fillTaskDevices("${t.id}")'>Fill devices (${fillN})</button>`
  :(incN?`<span class="fill-ep-hint" title="Connect the grabettes that recorded these episodes to repair them">${warnIcon()} ${incN} incomplete — connect their grabettes</span>`:'');
 // Pairing-issues accordion — only for a task that currently has orphans. Same
 // style as the global banner, nested under the Episodes accordion.
 const g=orphanFor(t.name);
 const issuesOpen=issuesOpenId===t.id;
 const issues=g?`<div class="hist-toggle warn" onclick='toggleIssues("${t.id}")'>${issuesOpen?'▾':'▸'} ${warnIcon()} Pairing issues (${g.count})</div>${issuesOpen?orphanCardHtml(g):''}`:'';
 // Device groups: which device combinations recorded this task, and how many
 // episodes each. From connected devices' reports (same scope as Episodes).
 const combos=taskCombos(t);
 const groupsOpen=groupsOpenId===t.id;
 const groups=combos.length?`<div class="hist-toggle" onclick='toggleGroups("${t.id}")'>${groupsOpen?'▾':'▸'} Device groups (${combos.length})</div>${groupsOpen?combos.map(comboRowHtml).join(''):''}`:'';
 const connNote='<span class="muted" style="font-weight:400"> · from connected devices</span>';
 return `<div class="task-detail">
   <div class="ds-step" style="margin:.2rem 0 .6rem"><span class="step-num">2</span> Select the devices to use</div>
   <div class="task-launch">${launchRolesHtml(t)}<div class="editor-actions">${launchBtn}</div></div>
   ${groups}
   <div class="hist-head"><div class="hist-toggle" onclick='toggleHistory("${t.id}")'>${histOpen?'▾':'▸'} Episodes (${eps.length})${connNote}</div>${fillCtrl}</div>
   ${histOpen?epRows:''}
   ${issues}
  </div>`;}
function taskEpsHtml(total){return `<div class="task-eps"><span class="ep-count" title="Total episodes recorded for this task">${total} ${total===1?'episode':'episodes'}</span></div>`;}
function taskSubHtml(t){return `<div class="task-sub"><span class="muted">Required devices:</span> ${sigBadges(t.device_signature)}${t.description?` · ${esc(t.description)}`:''}</div>`;}
function renderTaskList(){
 // Don't clobber an inline edit-in-progress on the periodic refresh — rebuilding
 // innerHTML would wipe what the user is typing. Skip while its editor is live.
 if(taskEditId && $('edit-name'))return;
 const el=$('task-list');$('count-tasks').textContent=TASKS.length;
 const step1=$('ds-step1');
 if(!TASKS.length){el.className='muted empty';el.textContent='No tasks yet.';step1.style.display='none';renderAdvanced();renderDatasetBar();return;}
 el.className='';
 // Step 1 header, styled the same in both modes but worded for the task at hand.
 step1.style.display='flex';
 step1.innerHTML='<span class="step-num">1</span> '+(selectMode?'Select tasks to include':'Select the task to record');
 renderAdvanced();
 if(selectMode){
  el.innerHTML=TASKS.map(t=>{
   const checked=datasetSel.has(t.id);
   const disabled=!checked&&!datasetCompatible(t);
   return `<div class="task-block">
    <div class="task-row select${disabled?' disabled':''}" ${disabled?'':`onclick='toggleDatasetPick("${t.id}")'`}>
     <div class="task-top">
      <input type="checkbox" class="task-check" ${checked?'checked':''} ${disabled?'disabled':''}>
      <span class="tname">${esc(t.name)}</span>
     </div>
     ${taskEpsHtml(t.episode_count||0)}
     ${taskSubHtml(t)}
    </div>
   </div>`;
  }).join('');
  renderDatasetBar();return;
 }
 el.innerHTML=TASKS.map(t=>{
  const editing=taskEditId===t.id;
  const deleting=taskDeleteId===t.id;
  const detail=editing?taskEditorHtml(t):(deleting?taskDeleteHtml(t):(expandedTaskId===t.id?taskDetailHtml(t):''));
  const hasIssues=!!orphanFor(t.name);
  return `<div class="task-block">
    <div class="task-row" ${(editing||deleting)?'':`onclick='toggleTaskDetail("${t.id}")'`}>
     <div class="task-top">
      <span class="tname">${esc(t.name)}${hasIssues?`<span class="task-warn" title="Episode pairing issues">${warnIcon()}</span>`:''}</span>
      <div class="task-acts">
       <button class="act-edit" title="Edit task" onclick='event.stopPropagation();editTask("${t.id}")'>
        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20h9"/><path d="M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4Z"/></svg>
       </button>
       <button class="del-icon" title="Delete task" onclick='event.stopPropagation();deleteTask("${t.id}")'>
        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>
       </button>
      </div>
     </div>
     ${taskEpsHtml(t.episode_count||0)}
     ${taskSubHtml(t)}
    </div>${detail}
   </div>`;
 }).join('');
 renderDatasetBar();}
// ── Sessions ──
// The launcher now lives inside each task's expanded detail (taskDetailHtml):
// pick a device per required role, then Launch. A single session runs at a time.
async function launchSession(taskId){
 const t=TASKS.find(x=>x.id===taskId);if(!t)return;
 const body={task_id:taskId};
 for(const role of (t.device_signature||[])){
  const id=launchRolePick[role];
  if(!id){alert('Select a device for '+ROLE_LABEL[role]);return;}
  if(!deviceOnline(id)){alert(deviceName(id)+' is offline — a session needs every device online.');return;}
  body[role]=id;
 }
 const r=await fetch('/api/fleet/sessions',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});
 if(!r.ok){const j=await r.json().catch(()=>({}));alert(errText(j)||'Failed to launch session');return;}
 expandedTaskId='';launchRolePick={left:'',right:'',casquette:''};await refresh();}
// Single record toggle (phone-camera style): start when idle, stop when
// recording/initializing; ignored mid-stop.
function episodeToggle(id){
 const s=SESSIONS.find(x=>x.id===id);if(!s)return;
 const p=sessionPhase(s);
 if(p==='idle')episodeStart(id);
 else if(p==='recording'||p==='initializing')episodeStop(id);}
async function episodeStart(id){
 const r=await fetch(`/api/fleet/sessions/${id}/episode/start`,{method:'POST'});
 const j=await r.json().catch(()=>({}));
 if(!r.ok){alert(errText(j)||'Failed to start episode');return;}
 await refresh();}  // the "Initializing…" state shows the pre-T0 wait — no alert needed
async function episodeStop(id){await fetch(`/api/fleet/sessions/${id}/episode/stop`,{method:'POST'});await refresh();}
async function deleteLastEpisode(id){
 if(!confirm('Delete the last episode of this session on ALL its devices? This cannot be undone.'))return;
 const r=await fetch(`/api/fleet/sessions/${id}/episode/delete-last`,{method:'POST'});
 if(!r.ok){const j=await r.json().catch(()=>({}));alert(errText(j)||'Failed to delete episode');return;}
 await refresh();}
async function fillTaskDevices(taskId){
 const r=await fetch(`/api/fleet/tasks/${encodeURIComponent(taskId)}/fill-devices`,{method:'POST'});
 if(!r.ok){const j=await r.json().catch(()=>({}));alert(errText(j)||'Failed to fill devices');return;}
 await refresh();}
async function closeSession(id){if(!confirm('Close this session?'))return;await fetch(`/api/fleet/sessions/${id}/close`,{method:'POST'});await refresh();}
async function deleteSession(id){if(!confirm('Delete this session and its manifest?'))return;await fetch(`/api/fleet/sessions/${id}`,{method:'DELETE'});await refresh();}
// The session's capture phase, mirroring the grabette LEDs (off/blink/solid/
// fast-blink): idle → nothing running; initializing → episode dispatched, still
// before the shared T0 (devices warming/waiting); recording → past T0; stopping
// → devices tearing down + muxing. initializing↔recording is derived from T0;
// stopping is reported by the fleet and ends when the devices actually finish
// (s.stopping is cleared as each stop_capture reports its result).
function sessionPhase(s){
 if(s.recording){
  const ep=(s.episodes&&s.episodes.length)?s.episodes[s.episodes.length-1]:null;
  const t0=ep&&ep.start_at_utc?Date.parse(ep.start_at_utc):0;
  return (t0&&Date.now()<t0)?'initializing':'recording';
 }
 return s.stopping?'stopping':'idle';}
function phasePill(s){
 // State pill for the panel header — the recording duration lives above the
 // record button, not in the pill.
 const p=sessionPhase(s);
 if(p==='recording')return '<span class="pill rec">● Recording</span>';
 if(p==='initializing')return '<span class="pill init">◌ Initializing…</span>';
 if(p==='stopping')return '<span class="pill stopping">◍ Stopping…</span>';
 return '<span class="pill idle">○ Idle</span>';}
// Episodes that lost their pair (a peer deleted the task while this device was
// offline; on reconnect its copies are orphaned). Cleanup deletes only the ones
// whose deleting peer is back online — those still paired with an offline device
// are left untouched.
function orphanFor(name){return ORPHANS.find(g=>g.task===name)||null;}  // group for a task, or null
function orphanCardHtml(g){
 const i=ORPHANS.indexOf(g);
 const dels=g.deleted_by.map(esc).join(', ')||'a peer';
 const hold=g.holders.map(esc).join(', ');
 const head=g.whole_task
  ?`<b>${esc(g.task)}</b> — task deleted on <b>${dels}</b>; ${g.count} episode(s) still here`
  :`<b>${esc(g.task)}</b> — ${g.count} episode(s) lost their pair`;
 const detail=g.whole_task
  ?`This task was deleted (with its recordings) on <b>${dels}</b>, but ${g.count} orphaned episode(s) remain on <b>${esc(hold)}</b>.`
  :`Deleted on <b>${dels}</b>, still present on <b>${esc(hold)}</b>.`;
 const btn=g.whole_task
  ?`Delete the task and its ${g.count} episode(s) on ${esc(hold)}`
  :`Delete the ${g.count} orphaned episode(s) on ${esc(hold)}`;
 return `<div class="orphan-item">
    <div class="orphan-head">${head}</div>
    <div class="orphan-detail">${detail}</div>
    <button class="del-confirm" onclick="cleanupOrphans(${i})">${btn}</button>
   </div>`;}
// Collapsed accordion under the tabs summarising all pairing issues.
function renderOrphans(){
 const bar=$('orphan-bar');
 if(!ORPHANS.length){bar.style.display='none';bar.innerHTML='';orphansOpen=false;return;}
 bar.style.display='block';
 const n=ORPHANS.length;
 bar.innerHTML=`<div class="orphan-acc">
   <div class="orphan-acc-head" onclick="orphansOpen=!orphansOpen;renderOrphans()">
    <span>${warnIcon()} ${n} task${n>1?'s':''} with episode pairing issues</span>
    <span class="orphan-caret">${orphansOpen?'▾':'▸'}</span>
   </div>
   ${orphansOpen?`<div class="orphan-acc-body">${ORPHANS.map(orphanCardHtml).join('')}</div>`:''}
  </div>`;}
async function cleanupOrphans(i){
 const g=ORPHANS[i];if(!g)return;
 if(!confirm(`Permanently delete ${g.episode_ids.length} orphaned episode(s) of “${g.task}”? Their pair was already removed, so they can't complete a dataset.`))return;
 const r=await fetch('/api/fleet/orphans/cleanup',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({episode_ids:g.episode_ids})});
 if(!r.ok){alert('Failed to clean up orphaned episodes');return;}
 await refresh();}
function renderSessionList(){
 // A single running session REPLACES the task list: when one is open, show the
 // session card and hide the mode tabs + Tasks card (the Fleet accordion in the
 // sticky bar stays); otherwise restore them and hide the (empty) session card.
 const open=SESSIONS.filter(s=>s.status==='open');
 const el=$('session-list'),card=$('sessions-card'),tasksCard=$('tasks-card'),tabs=$('page-tabs');
 if(!open.length){
  card.style.display='none';
  tasksCard.style.display='';
  if(tabs)tabs.style.display='';
  return;
 }
 card.style.display='';
 tasksCard.style.display='none';
 if(tabs)tabs.style.display='none';
 el.className='';
 el.innerHTML=open.map(s=>{
  const devs=Object.entries(s.members).map(([role,m])=>{
   const url=`http://${encodeURIComponent(m.name)}.local:8000`;
   return `<span class="sp-dev"><span class="pill hand" title="${role}">${role[0].toUpperCase()}</span> ${esc(m.name)}${deviceOnline(m.device_id)?batteryPill(deviceBattery(m.device_id)):''}${deviceBusyForRec(m.device_id)?activityBadge(m.device_id):''}`+
    ` <a class="dash-icon" target="_blank" rel="noopener" title="Open ${esc(m.name)} dashboard" href="${url}"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/></svg></a></span>`;
  }).join('');
  const p=sessionPhase(s);
  const ep=(s.episodes&&s.episodes.length)?s.episodes[s.episodes.length-1]:null;
  const t0=ep&&ep.start_at_utc?Date.parse(ep.start_at_utc):0;
  // Count only FINISHED episodes: the in-progress one (recording) isn't counted yet.
  const done=Math.max(0,(s.episode_count||0)-(s.recording?1:0));
  const M={
   idle:{lbl:'Idle',cls:'idle',btn:'idle',dis:'',title:'Start episode'},
   initializing:{lbl:'Initializing…',cls:'init',btn:'busy',dis:'',title:'Cancel'},
   recording:{lbl:'Recording',cls:'rec',btn:'recording',dis:'',title:'Stop episode'},
   stopping:{lbl:'Stopping…',cls:'stopping',btn:'busy',dis:'disabled',title:'Stopping…'},
  }[p];
  const timer=(p==='recording'&&t0)?`<span class="sp-timer rec-dur" data-start="${t0}">${fmtDur(Date.now()-t0)}</span>`:'';
  const task=esc(s.task_name||'(task deleted)');
  // A member busy with a dataset upload/conversion blocks STARTING a new episode
  // (mirrors the server gate); an in-progress recording is left alone.
  const busyMembers=Object.values(s.members).map(m=>m.device_id).filter(deviceBusyForRec);
  const recBlocked=(p==='idle')&&busyMembers.length>0;
  const circDis=M.dis||(recBlocked?'disabled':'');
  const circTitle=recBlocked?'A device is busy processing a dataset — wait for it to finish':M.title;
  const circle=`<div class="sp-rec"><button class="rec-toggle ${M.btn}${recBlocked?' blocked':''}" ${circDis} title="${circTitle}" onclick='episodeToggle("${s.id}")'><span class="rt-inner"></span></button></div>`;
  const busyNote=recBlocked?`<div class="sp-busy-note">${busyMembers.map(deviceName).join(', ')} busy processing a dataset — recording paused until it finishes.</div>`:'';
  const del=`<button class="del-ep" ${(p!=='idle'||!done)?'disabled':''} title="Delete the last episode on all its devices" onclick='deleteLastEpisode("${s.id}")'>Delete last episode</button>`;
  const close=`<button class="close-btn" onclick='closeSession("${s.id}")'>Close session</button>`;
  const epWord=`episode${done>1?'s':''}`;
  return `
   <div class="session-panel var-c">
    <div class="sp-rec-zone">
     <div class="sp-head"><span class="sp-task">${task}</span>${phasePill(s)}</div>
     <div class="sp-timer-wrap">${timer}</div>
     ${circle}
     ${busyNote}
     <div class="sp-rec-del">${del}</div>
    </div>
    <div class="sp-manage-zone">
     <div class="sp-section-label">Episodes</div>
     <div class="ep-big"><span class="ep-big-num">${done}</span><span class="ep-big-lbl">${epWord} recorded this session</span></div>
     <div class="sp-meta"><span class="sp-label">Devices</span><span class="sp-devs">${devs}</span></div>
     ${close}
    </div>
   </div>`;
 }).join('');
 tickRecDur();}
const openRaw=new Set();
function rawToggle(id,open){open?openRaw.add(id):openRaw.delete(id);}
const showState=new Set();
function runState(id){showState.add(id);dispatch(id,'get_state',{});}
function hideState(id){showState.delete(id);refresh();}
function kindOf(d){
 const s=`${d.name||''} ${d.device_id||''} ${(d.capabilities||[]).join(' ')}`.toLowerCase();
 if(s.includes('gripette'))return 'gripette';
 if(s.includes('casquette'))return 'casquette';
 return 'grabette';}
function btn(id,type,args,online){const dis=online?'':'disabled';return `<button class="primary" ${dis} onclick='dispatch("${id}","${type}",${JSON.stringify(args||{})})'>${type}</button>`;}
async function dispatch(device_id,type,args){
 await fetch('/api/fleet/dispatch',{method:'POST',headers:{'Content-Type':'application/json'},
  body:JSON.stringify({device_id,type,args})});refresh();}
async function removeDevice(device_id){
 await fetch(`/api/fleet/devices/${device_id}/remove`,{method:'POST'});refresh();}
async function checkLogin(){
 // Never leave #who stuck on "Checking…": if the request fails (the free-tier
 // Space is waking up, a redeploy is in flight, or a transient network error),
 // show a retrying message and let the 3s tick recover instead of throwing.
 try{
  const r=await fetch('/api/fleet/me');
  if(!r.ok)throw new Error('HTTP '+r.status);
  const m=await r.json();loggedIn=m.logged_in;
  if(loggedIn){$('who').innerHTML=`<div class="who-row"><span>Signed in as <b>${m.username}</b></span><a class="btn logout" href="/oauth/huggingface/logout">Logout</a></div>`;}
  else{$('who').innerHTML=`<a class="btn primary" href="/oauth/huggingface/login">Sign in with HuggingFace</a>`;}
 }catch(e){
  loggedIn=false;
  $('who').innerHTML=`<span class="muted">Connecting to the Space… <span style="opacity:.7">(it may be waking up — retrying)</span></span>`;
 }
 $('gated').disabled=!loggedIn;}
function esc(s){return String(s).replace(/[&<>]/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;'}[c]));}
function cap(s){return String(s).replace(/(^|_)([a-z])/g,(m,p,c)=>p+c.toUpperCase());}
function kv(k,v){return `<div class="k">${esc(k)}</div><div class="v">${v}</div>`;}
function fmtState(st){
 const rows=[];
 rows.push(kv('Daemon',`${esc(st.state||'?')}${st.backend?` · <span class="muted">${esc(st.backend)}</span>`:''}`));
 const sens=st.sensor||{};const cap=sens.capture||{};
 if(cap.is_capturing){
  const bits=[];
  if(cap.duration_seconds!=null)bits.push(`${Math.round(cap.duration_seconds)}s`);
  if(cap.frame_count!=null)bits.push(`${cap.frame_count} frames`);
  rows.push(kv('Capture',`<span class="pill rec">● Recording</span> ${esc(bits.join(' · '))}`));
  if(cap.session_id)rows.push(kv('Session',esc(cap.session_id)));
 }else if(cap.is_starting){
  rows.push(kv('Capture',`<span class="pill idle">○ Starting…</span>`));
 }else{
  rows.push(kv('Capture',`<span class="pill idle">○ Idle</span>`));
 }
 if(sens.angle){const deg=r=>(Number(r)*180/Math.PI).toFixed(1);
  rows.push(kv('Angle',`prox ${deg(sens.angle.proximal)}° · dist ${deg(sens.angle.distal)}°`));}
 if(st.error)rows.push(kv('Error',`<span class="err">${esc(st.error)}</span>`));
 return `<div class="kv">${rows.join('')}</div>`;}
function formatResult(type,res){
 if(res==null)return '<span class="muted">no result</span>';
 if(res.status==='error')return `<div class="err">⚠ ${esc(res.message||'error')}</div>`;
 if(type==='get_state'&&res.state)return fmtState(res.state);
 if(type==='start_capture')return `<div>▶ Capture started${res.episode_id?` · <span class="muted">${esc(res.episode_id)}</span>`:''}</div>`;
 if(type==='stop_capture'){const r=res.result||{};const bits=[];
  if(r.frame_count!=null)bits.push(`${r.frame_count} frames`);
  if(r.duration_seconds!=null)bits.push(`${Math.round(r.duration_seconds)}s`);
  return `<div>■ Capture stopped${bits.length?` · <span class="muted">${esc(bits.join(' · '))}</span>`:''}</div>`;}
 if(type==='logout')return '<div>Logged out</div>';
 return `<pre>${esc(JSON.stringify(res,null,1))}</pre>`;}
function rowHtml(d){
 const last=d.history[0];
 const caps=d.capabilities||[];
 const hasLogout=caps.includes('logout');
 const logoutDis=d.online?'':'disabled';
 const logoutBtn=hasLogout
  ?`<button class="logout-icon" title="Logout" ${logoutDis} onclick='dispatch("${d.device_id}","logout",{})'><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/></svg></button>`
  :'';
 const deleteBtn=`<button class="del-icon" title="Remove device" onclick='removeDevice("${d.device_id}")'><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg></button>`;
 const dashUrl=`http://${encodeURIComponent(d.name)}.local:8000`;
 const dashBtn=`<a class="dash-icon" target="_blank" rel="noopener" title="Open device dashboard (${dashUrl})" href="${dashUrl}"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/></svg></a>`;
 const handBadge=d.hand?`<span class="pill hand" title="${esc(d.hand)} hand">${d.hand==='left'?'L':d.hand==='right'?'R':esc(d.hand)}</span>`:'';
 const showRes=last&&!(last.type==='get_state'&&!showState.has(d.device_id));
 const rtime=last&&last.ts?new Date(last.ts*1000).toLocaleTimeString():'';
 const closeBtn=last&&last.type==='get_state'?`<button class="rclose" title="Close" onclick='hideState("${d.device_id}")'>×</button>`:'';
 const resultHtml=showRes
  ?`<div class="result"><div class="rlabel">${esc(cap(last.type))}${rtime?` · ${esc(rtime)}`:''}${closeBtn}</div>${formatResult(last.type,last.result)}`
   +`<details class="raw-d"${openRaw.has(d.device_id)?' open':''} ontoggle='rawToggle("${d.device_id}",this.open)'><summary>raw</summary><pre>${esc(JSON.stringify(last.result,null,1))}</pre></details></div>`
  :'<div class="result empty muted">No command run yet.</div>';
 return `<tr>
   <td class="c-dot"><span class="dot ${d.online?'on':'off'}" title="${d.online?'online':'offline'}"></span></td>
   <td class="name"><b>${d.name}</b>${handBadge}${d.online?batteryPill(d.battery):''}${d.online?activityBadge(d.device_id):''}<br><span class="muted">${d.device_id}${d.ip?` · ${esc(d.ip)}`:''}${d.pending?` · ${d.pending} pending`:''}</span>${resultHtml}</td>
   <td class="c-tools"><div class="tools">${dashBtn}${logoutBtn}${deleteBtn}</div></td></tr>`;}
async function refresh(){
 if(!loggedIn)return;
 const [dr,tr,sr,or_]=await Promise.all([fetch('/api/fleet/devices'),fetch('/api/fleet/tasks'),fetch('/api/fleet/sessions'),fetch('/api/fleet/orphans')]);
 if(dr.ok){DEVICES=(await dr.json()).devices;}
 if(tr.ok){TASKS=(await tr.json()).tasks;}
 if(sr.ok){SESSIONS=(await sr.json()).sessions;}
 if(or_.ok){ORPHANS=(await or_.json()).groups;}
 // Fleet tables (Devices must be applied before the launcher/roles render).
 const kinds={grabette:[],gripette:[],casquette:[]};
 for(const d of DEVICES)kinds[kindOf(d)].push(d);
 for(const k of KINDS){
  const list=kinds[k];
  $('tb-'+k).innerHTML=list.map(rowHtml).join('');
  $('count-'+k).textContent=list.length;
  $('empty-'+k).style.display=list.length?'none':'block';}
 renderFleetRecap();
 // Tasks (with inline launcher) + the running session.
 renderTaskList();
 renderSessionList();
 renderOrphans();}
async function tick(){
 try{await checkLogin();}catch(e){}
 try{await refresh();}catch(e){}
}
tick();setInterval(tick,3000);
// 1s UI tick. While a session is open, poll session state at 1s (not the 3s
// full refresh) so phase changes show promptly: initializing→recording (at T0)
// and, crucially, stopping→idle the moment the fleet marks the devices' stop
// complete — instead of lagging up to 3s. Devices/tasks stay on the 3s refresh.
async function uiTick(){
 if(SESSIONS.some(s=>s.status==='open')){
  try{const r=await fetch('/api/fleet/sessions');if(r.ok)SESSIONS=(await r.json()).sessions;}catch(e){}
  renderSessionList();
 }
 tickRecDur();
}
setInterval(uiTick,1000);
</script></body></html>"""