File size: 115,945 Bytes
10e9b7d
f079cb0
4e424f4
631e5ed
43960d9
631e5ed
 
43960d9
f079cb0
 
 
95a8cae
10e9b7d
3c4371f
95a8cae
f079cb0
 
 
c991e6f
b09ef8c
f079cb0
10e9b7d
95a8cae
3db6293
95a8cae
f079cb0
631e5ed
f39cf2c
 
9533afb
43960d9
 
9535c3c
631e5ed
 
43960d9
 
3c69123
631e5ed
 
 
 
 
 
 
3c69123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631e5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9533afb
 
 
 
631e5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9533afb
 
 
 
 
 
 
 
 
 
 
631e5ed
 
 
f079cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fd5127
 
 
631e5ed
 
 
3fd5127
 
 
631e5ed
 
 
3fd5127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631e5ed
 
 
3fd5127
 
 
 
 
 
 
 
 
 
 
9535c3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631e5ed
 
 
f079cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e424f4
 
 
 
 
 
 
 
 
f079cb0
 
 
 
 
4e424f4
631e5ed
 
 
 
f079cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631e5ed
 
 
 
 
 
 
 
 
 
f079cb0
 
 
43960d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9533afb
43960d9
9533afb
43960d9
 
 
 
 
 
9533afb
43960d9
 
 
 
 
 
 
 
9535c3c
 
 
 
 
f39cf2c
 
 
9535c3c
 
 
 
 
631e5ed
 
 
 
 
 
 
 
9535c3c
 
 
631e5ed
9535c3c
 
 
631e5ed
9535c3c
 
631e5ed
 
9535c3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f39cf2c
9535c3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f39cf2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9533afb
f39cf2c
 
631e5ed
 
 
 
9535c3c
631e5ed
9535c3c
 
 
 
631e5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9533afb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631e5ed
 
 
 
 
43960d9
631e5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43960d9
 
 
 
631e5ed
43960d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631e5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f079cb0
 
 
 
631e5ed
 
f079cb0
 
 
 
 
631e5ed
 
 
 
 
f079cb0
 
 
631e5ed
f079cb0
 
 
 
 
3fd5127
631e5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f079cb0
 
631e5ed
 
 
 
95a8cae
e80aab9
4e424f4
 
 
 
 
 
 
 
 
 
 
631e5ed
 
 
 
 
4e424f4
 
 
631e5ed
4e424f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631e5ed
 
 
 
 
4e424f4
631e5ed
 
 
 
4e424f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fd5127
 
 
 
 
 
 
 
 
 
4e424f4
 
 
 
 
c991e6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631e5ed
 
 
c991e6f
 
4e424f4
c991e6f
 
 
 
 
 
 
 
 
 
631e5ed
c991e6f
 
4e424f4
c991e6f
 
 
4e424f4
631e5ed
 
 
 
4e424f4
 
31243f4
 
95a8cae
 
b358dff
43960d9
1b12e7f
 
c991e6f
43960d9
c991e6f
43960d9
c991e6f
 
 
 
43960d9
b358dff
43960d9
1b12e7f
b358dff
 
95a8cae
1b12e7f
43960d9
513b67b
43960d9
 
95a8cae
513b67b
1b12e7f
 
83c484b
43960d9
9535c3c
83c484b
 
 
 
 
 
 
 
 
 
 
1b12e7f
83c484b
 
9533afb
 
631e5ed
83c484b
1b12e7f
631e5ed
 
1b12e7f
 
631e5ed
1b12e7f
3c69123
 
1b12e7f
43960d9
c991e6f
 
1b12e7f
b9224d4
9533afb
43960d9
9535c3c
95a8cae
 
 
 
 
 
 
 
83c484b
 
 
631e5ed
9533afb
 
 
631e5ed
 
 
 
 
 
 
 
 
 
83c484b
 
 
 
3c69123
 
 
631e5ed
 
9533afb
 
83c484b
 
c991e6f
 
9535c3c
 
83c484b
 
 
 
 
 
95a8cae
9827274
 
 
 
b9224d4
 
9533afb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c69123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9533afb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c69123
9533afb
 
 
 
3c69123
 
 
 
 
 
 
 
 
9533afb
 
 
 
 
 
 
 
 
 
 
 
f079cb0
95a8cae
 
 
43960d9
3c69123
 
95a8cae
9535c3c
 
631e5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9535c3c
 
 
 
 
 
 
3c69123
 
 
 
 
 
 
 
 
 
 
 
 
 
b358dff
3c69123
 
 
 
 
b358dff
 
 
 
 
 
 
 
1b12e7f
82e979c
 
b358dff
 
c991e6f
9533afb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c69123
 
 
9533afb
 
 
 
 
 
 
 
 
3c69123
9533afb
3c69123
 
 
 
 
 
 
 
 
 
 
 
 
9533afb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c69123
 
 
9533afb
 
 
 
 
 
 
 
43960d9
83c484b
 
 
3c69123
83c484b
513b67b
 
 
 
 
c991e6f
513b67b
 
c991e6f
 
 
 
 
 
 
 
 
 
 
513b67b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c991e6f
513b67b
 
 
 
 
 
 
 
 
c991e6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631e5ed
 
 
 
 
 
 
 
 
 
 
 
c991e6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f39cf2c
 
 
 
 
 
 
 
 
 
43960d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c991e6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
513b67b
4e424f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
513b67b
4e424f4
 
 
 
 
513b67b
4e424f4
95a8cae
43960d9
3c69123
 
 
 
 
83c484b
43960d9
 
 
c991e6f
43960d9
9535c3c
 
 
83c484b
43960d9
83c484b
43960d9
 
83c484b
43960d9
 
 
 
3c69123
 
43960d9
631e5ed
 
3c69123
 
83c484b
 
43960d9
 
 
 
 
 
 
 
 
 
 
 
3fd5127
 
43960d9
3fd5127
 
43960d9
9533afb
 
43960d9
 
 
 
9533afb
 
 
 
 
 
 
 
 
 
 
43960d9
9533afb
43960d9
 
 
 
 
9535c3c
43960d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83c484b
95a8cae
 
 
 
 
f079cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b09ef8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1ecd1b
 
 
b09ef8c
 
 
a1ecd1b
 
 
 
 
b09ef8c
 
 
a1ecd1b
 
b09ef8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1ecd1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c69123
 
 
 
a1ecd1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f079cb0
 
95a8cae
f079cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95a8cae
f079cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95a8cae
b9224d4
 
95a8cae
b9224d4
f079cb0
 
 
 
 
e80aab9
31243f4
 
95a8cae
f079cb0
 
 
 
 
3c4371f
95a8cae
eccf8e4
95a8cae
7d65c66
95a8cae
 
f079cb0
 
 
 
 
95a8cae
f079cb0
 
 
 
 
95a8cae
 
 
31243f4
95a8cae
 
31243f4
95a8cae
31243f4
f079cb0
95a8cae
3c69123
 
 
 
 
95a8cae
 
 
 
 
 
 
 
 
 
f079cb0
 
 
 
 
95a8cae
 
 
 
 
 
f079cb0
 
95a8cae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9224d4
95a8cae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9224d4
95a8cae
e80aab9
95a8cae
 
 
 
 
e80aab9
95a8cae
 
 
 
 
 
 
 
e80aab9
95a8cae
 
 
 
 
 
 
e80aab9
 
95a8cae
 
0ee0419
95a8cae
 
e80aab9
7e4a06b
e80aab9
95a8cae
 
 
f079cb0
 
95a8cae
f079cb0
95a8cae
f079cb0
95a8cae
f079cb0
95a8cae
f079cb0
 
 
 
 
 
 
95a8cae
 
 
 
f079cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95a8cae
 
f079cb0
 
95a8cae
e80aab9
95a8cae
 
b09ef8c
 
 
 
 
 
 
a1ecd1b
 
 
 
 
 
 
b09ef8c
 
 
 
 
 
 
 
 
95a8cae
b09ef8c
 
 
 
 
 
 
 
 
 
a1ecd1b
b09ef8c
 
 
95a8cae
b09ef8c
 
 
 
 
f079cb0
 
 
 
 
 
95a8cae
 
 
 
 
 
b09ef8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95a8cae
a1ecd1b
 
 
 
 
 
 
 
 
 
 
b09ef8c
 
 
 
 
 
 
 
 
 
 
 
 
f079cb0
b09ef8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f079cb0
b09ef8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95a8cae
b9224d4
95a8cae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e80aab9
 
 
95a8cae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
import os
import re
import base64
import ast
import json
import math
import operator
import threading
from io import BytesIO
from pathlib import Path
from zipfile import ZipFile

import gradio as gr
import pandas as pd
import requests
from smolagents import (
    LiteLLMModel,
    Tool,
    ToolCallingAgent,
    WikipediaSearchTool,
)


DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
RESULT_COLUMNS = ["Task ID", "Question", "Submitted Answer"]
HTTP_TIMEOUT = 45
MAX_EXTRACTED_CHARS = 8_000
WEBPAGE_CONNECT_TIMEOUT = 8
WEBPAGE_READ_TIMEOUT = 20
MAX_WEBPAGE_CHARS = 3_500
DEFAULT_MAIN_MODEL = "cerebras/zai-glm-4.7"
DEFAULT_GEMINI_REVIEW_MODEL = "gemini-3.5-flash"
TASK_FILE_CACHE = {}
ATTACHMENT_CACHE = {}
WEBPAGE_CACHE = {}
SEARCH_CACHE = {}
SEARCH_LOCK = threading.Lock()
RUN_STATE = threading.local()


def compact_error(exc: Exception) -> str:
    message = str(exc).strip()
    return message or repr(exc)


def reset_run_evidence():
    RUN_STATE.evidence = []


def record_run_evidence(tool_name: str, value) -> str:
    text = str(value or "").strip()
    if text:
        evidence = getattr(RUN_STATE, "evidence", None)
        if evidence is None:
            evidence = []
            RUN_STATE.evidence = evidence
        evidence.append(
            {
                "tool": str(tool_name),
                "text": text[:5_000],
            }
        )
    return value


def current_run_evidence(max_chars: int = 14_000) -> str:
    items = getattr(RUN_STATE, "evidence", []) or []
    blocks = []
    used = 0
    for index, item in enumerate(items, start=1):
        block = f"[{index}. {item['tool']}]\n{item['text']}"
        remaining = max_chars - used
        if remaining <= 100:
            break
        blocks.append(block[:remaining])
        used += len(blocks[-1]) + 2
    return "\n\n".join(blocks)


def instrument_tool(tool: Tool) -> Tool:
    """Records tool observations for deterministic review and failover."""
    original_forward = tool.forward

    def recorded_forward(*args, **kwargs):
        result = original_forward(*args, **kwargs)
        return record_run_evidence(tool.name, result)

    tool.forward = recorded_forward
    return tool


def concise_query_from_question(question: str, max_terms: int = 14) -> str:
    stopwords = {
        "about", "answer", "attached", "could", "from", "give", "have",
        "into", "just", "number", "please", "provide", "question", "same",
        "that", "their", "there", "these", "this", "under", "what", "when",
        "where", "which", "with", "would", "your",
    }
    terms = [
        token
        for token in re.findall(r"[A-Za-z0-9][A-Za-z0-9'._-]{2,}", question or "")
        if token.casefold() not in stopwords
    ]
    return " ".join(terms[:max_terms])


def focus_text(text: str, query: str, max_chars: int = 5_000) -> str:
    """Selects high-signal passages locally, before text reaches the LLM."""
    text = re.sub(r"\r\n?", "\n", str(text or ""))
    text = re.sub(r"[ \t]+", " ", text)
    text = re.sub(r"\n{3,}", "\n\n", text).strip()
    if not text or len(text) <= max_chars:
        return text

    stopwords = {
        "about", "after", "again", "also", "article", "attached", "before",
        "could", "find", "from", "have", "into", "just", "mentions", "please",
        "provide", "question", "should", "that", "their", "there", "these",
        "this", "under", "what", "when", "where", "which", "with", "work",
        "would", "your",
    }
    terms = {
        token.lower()
        for token in re.findall(r"[A-Za-z0-9][A-Za-z0-9._-]{2,}", query or "")
        if token.lower() not in stopwords
    }
    blocks = [
        block.strip()
        for block in re.split(r"\n\s*\n", text)
        if block.strip()
    ]
    if not blocks:
        return text[:max_chars]

    scored = []
    for index, block in enumerate(blocks):
        lowered = block.lower()
        # Count distinct terms instead of every occurrence. This prevents a
        # huge navigation/table block containing the same year many times from
        # outranking a short passage containing the actual target terms.
        hits = sum(1 for term in terms if term in lowered)
        exact_bonus = 4 if query and query.lower() in lowered else 0
        signal_bonus = 2 if re.search(
            r"\b(acknowledg|award|grant|answer|result|total|page|pages)\b",
            lowered,
        ) else 0
        scored.append((hits * 3 + exact_bonus + signal_bonus, index))

    best_indices = [
        index
        for score, index in sorted(scored, reverse=True)
        if score > 0
    ][:10]
    if not best_indices:
        return (
            text[: max_chars * 2 // 3]
            + "\n\n[content omitted]\n\n"
            + text[-max_chars // 3 :]
        )

    selected = {}
    used = 0
    for best_index in best_indices:
        # Reserve space for the matching block before optional neighbors.
        for index in (best_index, best_index - 1, best_index + 1):
            if (
                index in selected
                or not 0 <= index < len(blocks)
                or used >= max_chars
            ):
                continue
            block = blocks[index]
            if index != best_index and len(block) > 1_000:
                block = block[:1_000]
            remaining = max_chars - used
            if remaining < 100:
                break
            if len(block) > remaining and terms:
                lowered_block = block.lower()
                positions = [
                    lowered_block.find(term)
                    for term in terms
                    if lowered_block.find(term) >= 0
                ]
                if positions:
                    center = min(positions)
                    start = max(0, center - remaining // 3)
                    block = block[start : start + remaining]
            selected[index] = block[:remaining]
            used += len(selected[index]) + 2
    return "\n\n".join(selected[index] for index in sorted(selected)).strip()


def clean_filename(value: str) -> str:
    value = Path(value).name
    return re.sub(r"[^A-Za-z0-9._-]+", "_", value) or "attachment"


def filename_from_response(response: requests.Response, task_id: str) -> str:
    disposition = response.headers.get("content-disposition", "")
    utf8_match = re.search(
        r"filename\*=UTF-8''([^;]+)", disposition, flags=re.IGNORECASE
    )
    plain_match = re.search(
        r'filename="?([^";]+)"?', disposition, flags=re.IGNORECASE
    )
    if utf8_match:
        from urllib.parse import unquote

        return clean_filename(unquote(utf8_match.group(1)))
    if plain_match:
        return clean_filename(plain_match.group(1))

    content_type = response.headers.get("content-type", "").lower()
    extensions = {
        "application/pdf": ".pdf",
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx",
        "text/csv": ".csv",
        "text/plain": ".txt",
        "application/json": ".json",
        "image/png": ".png",
        "image/jpeg": ".jpg",
    }
    extension = next(
        (ext for mime, ext in extensions.items() if mime in content_type), ""
    )
    return f"{clean_filename(task_id)}{extension}"


def download_gaia_attachment(task_id: str) -> tuple[bytes, str]:
    """Baixa um anexo pela API do curso, com fallback para o dataset oficial."""
    task_id = str(task_id).strip()
    if task_id in ATTACHMENT_CACHE:
        return ATTACHMENT_CACHE[task_id]

    course_url = f"{DEFAULT_API_URL}/files/{task_id}"
    response = requests.get(course_url, timeout=HTTP_TIMEOUT)
    if response.ok:
        result = (response.content, filename_from_response(response, task_id))
        ATTACHMENT_CACHE[task_id] = result
        return result
    if response.status_code != 404:
        response.raise_for_status()

    questions_response = requests.get(
        f"{DEFAULT_API_URL}/questions", timeout=HTTP_TIMEOUT
    )
    questions_response.raise_for_status()
    item = next(
        (
            question
            for question in questions_response.json()
            if str(question.get("task_id")) == task_id
        ),
        None,
    )
    filename = str((item or {}).get("file_name") or "").strip()
    if not filename:
        raise FileNotFoundError(f"No attachment exists for task {task_id}.")

    token = os.getenv("HF_TOKEN")
    if not token:
        raise RuntimeError(
            "The course file endpoint returned 404 and HF_TOKEN is required "
            "for the official GAIA dataset fallback."
        )

    from huggingface_hub import hf_hub_download

    errors = []
    for dataset_path in (
        f"2023/validation/{filename}",
        filename,
    ):
        try:
            local_path = hf_hub_download(
                repo_id="gaia-benchmark/GAIA",
                repo_type="dataset",
                filename=dataset_path,
                token=token,
            )
            result = (Path(local_path).read_bytes(), filename)
            ATTACHMENT_CACHE[task_id] = result
            return result
        except Exception as exc:
            errors.append(str(exc))

    raise RuntimeError(
        "Could not download the attachment from the course API or official "
        "GAIA dataset. Accept the dataset access conditions at "
        "https://huggingface.co/datasets/gaia-benchmark/GAIA and ensure "
        f"HF_TOKEN has read access. Details: {' | '.join(errors)}"
    )


def get_task_file_name(task_id: str) -> str:
    """Retorna o nome oficial do anexo ou uma string vazia."""
    task_id = str(task_id or "").strip()
    if not task_id:
        return ""
    if task_id in TASK_FILE_CACHE:
        return TASK_FILE_CACHE[task_id]

    try:
        response = requests.get(
            f"{DEFAULT_API_URL}/questions", timeout=HTTP_TIMEOUT
        )
        response.raise_for_status()
        for item in response.json():
            item_task_id = str(item.get("task_id", "")).strip()
            TASK_FILE_CACHE[item_task_id] = str(
                item.get("file_name") or ""
            ).strip()
    except Exception as exc:
        print(f"Could not load task attachment metadata: {exc}")
        return ""

    return TASK_FILE_CACHE.get(task_id, "")


def extract_attachment_text(
    data: bytes, filename: str, query: str = ""
) -> str:
    """Extrai conteúdo legível dos formatos mais comuns do GAIA."""
    suffix = Path(filename).suffix.lower()

    if suffix == ".pdf":
        from pypdf import PdfReader

        reader = PdfReader(BytesIO(data))
        text = "\n\n".join(page.extract_text() or "" for page in reader.pages)
    elif suffix == ".docx":
        from docx import Document

        document = Document(BytesIO(data))
        paragraphs = [paragraph.text for paragraph in document.paragraphs]
        for table in document.tables:
            for row in table.rows:
                paragraphs.append(" | ".join(cell.text for cell in row.cells))
        text = "\n".join(paragraphs)
    elif suffix in {".xlsx", ".xlsm"}:
        from openpyxl import load_workbook

        workbook = load_workbook(BytesIO(data), read_only=True, data_only=True)
        lines = []
        for sheet in workbook.worksheets:
            lines.append(f"--- Sheet: {sheet.title} ---")
            for row in sheet.iter_rows(values_only=True):
                if any(value is not None for value in row):
                    lines.append(
                        " | ".join(
                            "" if value is None else str(value) for value in row
                        )
                    )
        text = "\n".join(lines)
    elif suffix in {".csv", ".tsv"}:
        separator = "\t" if suffix == ".tsv" else ","
        dataframe = pd.read_csv(
            BytesIO(data), sep=separator, encoding_errors="replace"
        )
        text = dataframe.to_csv(index=False)
    elif suffix in {
        ".txt",
        ".md",
        ".json",
        ".html",
        ".htm",
        ".xml",
        ".py",
    }:
        text = data.decode("utf-8", errors="replace")
        if suffix in {".html", ".htm"}:
            from bs4 import BeautifulSoup

            text = BeautifulSoup(text, "html.parser").get_text("\n")
    elif suffix in {".mp3", ".wav", ".flac", ".m4a", ".ogg"}:
        text = (
            "Audio attachment detected. Use transcribe_gaia_audio with the "
            "task_id instead of inspect_gaia_attachment."
        )
    elif suffix == ".zip":
        with ZipFile(BytesIO(data)) as archive:
            text = "Files inside ZIP:\n" + "\n".join(archive.namelist())
    elif suffix in {".png", ".jpg", ".jpeg", ".webp", ".gif"}:
        from PIL import Image

        image = Image.open(BytesIO(data))
        text = (
            f"Image attachment: {filename}; format={image.format}; "
            f"size={image.width}x{image.height}. "
            "This text-only model cannot inspect image pixels reliably."
        )
    else:
        preview = data[:2_000].decode("utf-8", errors="replace")
        text = (
            f"Unsupported attachment format {suffix or '(unknown)'}. "
            f"Decoded preview:\n{preview}"
        )

    text = text.strip()
    if not text:
        return f"The attachment {filename} was downloaded but contained no extractable text."
    tabular = suffix in {".xlsx", ".xlsm", ".csv", ".tsv"}
    if query and not tabular:
        text = focus_text(text, query, MAX_EXTRACTED_CHARS)
    elif len(text) > MAX_EXTRACTED_CHARS:
        head_size = MAX_EXTRACTED_CHARS * 2 // 3
        text = (
            text[:head_size]
            + "\n\n[attachment middle omitted]\n\n"
            + text[-(MAX_EXTRACTED_CHARS - head_size) :]
        )
    return text


class ConciseWebSearchTool(Tool):
    name = "web_search"
    description = (
        "Searches the public web without an API key. Returns at most five "
        "compact results. Use one precise query, then open the best source. "
        "Identical repeated searches are skipped automatically."
    )
    inputs = {
        "query": {
            "type": "string",
            "description": "One concise web search query.",
        }
    }
    output_type = "string"

    def forward(self, query: str) -> str:
        from ddgs import DDGS

        query = " ".join(str(query or "").split())
        if not query:
            return "Search query is empty."
        cache_key = query.casefold()

        # The lock also prevents parallel duplicate searches from both sending
        # the same network request and duplicating a large observation.
        with SEARCH_LOCK:
            if cache_key in SEARCH_CACHE:
                return (
                    "Duplicate search skipped; the identical results are "
                    "already present in this run. Open one of those URLs."
                )
            try:
                raw_results = list(DDGS().text(query, max_results=4))
                lines = []
                for index, result in enumerate(raw_results[:4], start=1):
                    title = " ".join(str(result.get("title") or "").split())
                    url = str(result.get("href") or result.get("url") or "").strip()
                    snippet = " ".join(str(result.get("body") or "").split())
                    lines.append(
                        f"{index}. {title[:180]}\n"
                        f"URL: {url}\n"
                        f"Snippet: {snippet[:240]}"
                    )
                output = "\n\n".join(lines) or "No search results found."
                SEARCH_CACHE[cache_key] = output
                return output
            except Exception as exc:
                return f"Web search failed: {compact_error(exc)}"


class OpenWebPageTool(Tool):
    name = "visit_webpage"
    description = (
        "Opens and reads one exact HTTP/HTTPS page. Use it after web_search to "
        "verify article text, tables, archives, papers, and linked sources. "
        "It returns concise main-page content while preserving links near the "
        "end, and retries blocked HTML through a text mirror. It is not a "
        "binary-file downloader."
    )
    inputs = {
        "url": {
            "type": "string",
            "description": "The complete HTTP or HTTPS URL to open.",
        },
        "query": {
            "type": "string",
            "description": (
                "Short terms describing the exact fact to find on the page. "
                "Do not repeat the full task."
            ),
        },
    }
    output_type = "string"

    def forward(self, url: str, query: str) -> str:
        from markdownify import markdownify

        url = str(url or "").strip()
        query = str(query or "").strip()
        if not re.match(r"^https?://", url, flags=re.I):
            return "Invalid URL: visit_webpage requires a full HTTP/HTTPS URL."
        if url in WEBPAGE_CACHE:
            return focus_text(WEBPAGE_CACHE[url], query, MAX_WEBPAGE_CHARS)

        headers = {
            "User-Agent": (
                "Mozilla/5.0 (compatible; GAIA-Course-Agent/1.0; "
                "+https://huggingface.co/learn/agents-course)"
            )
        }
        errors = []
        targets = [url]
        if "r.jina.ai/http" not in url:
            without_scheme = re.sub(r"^https?://", "", url)
            targets.append(f"https://r.jina.ai/http://{without_scheme}")

        for target in targets:
            try:
                response = requests.get(
                    target,
                    headers=headers,
                    timeout=(WEBPAGE_CONNECT_TIMEOUT, WEBPAGE_READ_TIMEOUT),
                    allow_redirects=True,
                )
                response.raise_for_status()
                content_type = response.headers.get("content-type", "").lower()
                if (
                    "text/" not in content_type
                    and "json" not in content_type
                    and "xml" not in content_type
                ):
                    return (
                        f"The URL returned binary content ({content_type}). "
                        "Use inspect_gaia_attachment for official GAIA files."
                    )

                text = response.text
                if "html" in content_type:
                    from bs4 import BeautifulSoup
                    from trafilatura import extract
                    from urllib.parse import urljoin

                    soup = BeautifulSoup(text, "html.parser")
                    for element in soup.select(
                        "script, style, noscript, nav, header, footer, form, "
                        "aside, iframe"
                    ):
                        element.decompose()
                    main_content = (
                        soup.find("article")
                        or soup.find("main")
                        or soup.body
                        or soup
                    )
                    extracted = extract(
                        text,
                        url=target,
                        output_format="markdown",
                        include_comments=False,
                        include_tables=True,
                        include_links=True,
                        favor_recall=True,
                    )
                    text = (
                        extracted
                        if extracted and len(extracted.strip()) >= 200
                        else markdownify(str(main_content))
                    )

                    # Article extractors sometimes classify bibliography links
                    # as navigation. Append a compact link index so the agent
                    # can still open papers and primary sources cited at the end.
                    source_links = []
                    seen_links = set()
                    for anchor in main_content.find_all("a", href=True):
                        label = " ".join(anchor.get_text(" ", strip=True).split())
                        absolute_url = urljoin(target, anchor["href"])
                        if (
                            label
                            and absolute_url.startswith(("http://", "https://"))
                            and absolute_url not in seen_links
                        ):
                            seen_links.add(absolute_url)
                            source_links.append(
                                f"- [{label[:160]}]({absolute_url})"
                            )
                    if source_links:
                        text += (
                            "\n\nSource links found on the page:\n"
                            + "\n".join(source_links[-12:])
                        )
                text = re.sub(r"\n{3,}", "\n\n", text).strip()
                if text:
                    WEBPAGE_CACHE[url] = text[:50_000]
                    return focus_text(text, query, MAX_WEBPAGE_CHARS)
                return "The page was retrieved but contained no text."
            except Exception as exc:
                errors.append(f"{target}: {compact_error(exc)}")

        return "Error fetching the webpage: " + " | ".join(errors)


class ReadDocumentUrlTool(Tool):
    name = "read_document_url"
    description = (
        "Downloads and reads a PDF, DOCX, CSV, or text document from an exact "
        "public URL, then returns only passages relevant to the supplied query. "
        "Use it for linked papers and reports; use visit_webpage for HTML."
    )
    inputs = {
        "url": {
            "type": "string",
            "description": "Direct public URL of the document.",
        },
        "query": {
            "type": "string",
            "description": "Short terms for the exact fact to find.",
        },
    }
    output_type = "string"

    def forward(self, url: str, query: str) -> str:
        url = str(url or "").strip()
        query = str(query or "").strip()
        if not re.match(r"^https?://", url, flags=re.I):
            return "Invalid document URL."

        try:
            response = requests.get(
                url,
                headers={"User-Agent": "GAIA-Course-Agent/1.0"},
                timeout=(WEBPAGE_CONNECT_TIMEOUT, HTTP_TIMEOUT),
                allow_redirects=True,
            )
            response.raise_for_status()
            if len(response.content) > 25 * 1024 * 1024:
                return "Document exceeds the 25 MB safety limit."

            content_type = response.headers.get("content-type", "").lower()
            if "text/html" in content_type:
                return (
                    "This URL returned HTML. Use visit_webpage with the same "
                    "URL and a short query."
                )
            filename = filename_from_response(response, "web_document")
            if not Path(filename).suffix:
                from urllib.parse import urlparse

                filename = Path(urlparse(response.url).path).name or filename
            extracted = extract_attachment_text(
                response.content, filename, query=query
            )
            return f"Document: {filename}\n\n{extracted}"
        except Exception as exc:
            return f"Could not read document URL: {compact_error(exc)}"


class QueryWebTableTool(Tool):
    name = "query_web_table"
    description = (
        "Reads an HTML table and computes its minimum or maximum locally, "
        "without sending the full table to the model. Use it for rankings, "
        "counts, standings, statistics, and 'least/most' questions after a "
        "table URL is known. Column selectors may be 'first', 'last', a "
        "zero-based number, or visible header text. Ties are alphabetized."
    )
    inputs = {
        "url": {
            "type": "string",
            "description": "Exact HTTP/HTTPS page containing the table.",
        },
        "operation": {
            "type": "string",
            "description": "One of: describe, min, or max.",
        },
        "label_column": {
            "type": "string",
            "description": "Label column: usually first, or its header text.",
        },
        "value_column": {
            "type": "string",
            "description": "Numeric column: last, a zero-based index, or header text.",
        },
    }
    output_type = "string"

    @staticmethod
    def _number(value: str):
        cleaned = str(value or "").strip().replace(",", "")
        cleaned = re.sub(r"[%+$]", "", cleaned)
        if not re.fullmatch(r"-?\d+(?:\.\d+)?", cleaned):
            return None
        number = float(cleaned)
        return int(number) if number.is_integer() else number

    @staticmethod
    def _column_index(selector: str, headers: list[str], default: int) -> int:
        selector = str(selector or "").strip().casefold()
        if selector == "first":
            return 0
        if selector == "last":
            return -1
        if re.fullmatch(r"-?\d+", selector):
            return int(selector)
        matches = [
            index
            for index, header in enumerate(headers)
            if selector and selector in header.casefold()
        ]
        return matches[-1] if matches else default

    def forward(
        self,
        url: str,
        operation: str,
        label_column: str,
        value_column: str,
    ) -> str:
        from bs4 import BeautifulSoup

        url = str(url or "").strip()
        operation = str(operation or "").strip().lower()
        if not re.match(r"^https?://", url, flags=re.I):
            return "Invalid URL."
        if operation not in {"describe", "min", "max"}:
            return "Invalid operation. Use describe, min, or max."

        try:
            response = requests.get(
                url,
                headers={"User-Agent": "GAIA-Course-Agent/1.0"},
                timeout=(WEBPAGE_CONNECT_TIMEOUT, WEBPAGE_READ_TIMEOUT),
            )
            response.raise_for_status()
            soup = BeautifulSoup(response.text, "html.parser")
            tables = soup.find_all("table")
            if not tables:
                return "No HTML tables were found on this page."

            table = max(tables, key=lambda item: len(item.find_all("tr")))
            parsed_rows = []
            header_rows = []
            for row in table.find_all("tr"):
                cells = row.find_all(["th", "td"])
                values = [
                    " ".join(cell.get_text(" ", strip=True).split())
                    for cell in cells
                ]
                if not values:
                    continue
                if row.find("th"):
                    header_rows.append(values)
                parsed_rows.append(values)

            if not parsed_rows:
                return "The largest table contained no usable rows."
            headers = max(header_rows, key=len) if header_rows else parsed_rows[0]
            if operation == "describe":
                return (
                    f"Rows: {len(parsed_rows)}; approximate columns: "
                    f"{len(max(parsed_rows, key=len))}; headers: "
                    + " | ".join(headers[:80])
                    + ". Use first/last when multi-row headers are ambiguous."
                )[:3_000]

            label_index = self._column_index(label_column, headers, 0)
            value_index = self._column_index(value_column, headers, -1)
            candidates = []
            for row in parsed_rows:
                try:
                    label = row[label_index].strip()
                    value_text = row[value_index].strip()
                except IndexError:
                    continue
                value = self._number(value_text)
                if value is None or not label or self._number(label) is not None:
                    continue
                candidates.append((value, label, value_text))

            if not candidates:
                return (
                    "No numeric rows matched those columns. Call describe and "
                    "retry with first/last or a numeric column index."
                )
            candidates.sort(
                key=lambda item: (
                    item[0] if operation == "min" else -item[0],
                    item[1].casefold(),
                )
            )
            best_value = candidates[0][0]
            tied = [item for item in candidates if item[0] == best_value]
            preview = candidates[:10]
            return (
                f"{operation.upper()} result (ties alphabetized): "
                f"{tied[0][1]} = {tied[0][2]}\n"
                "Top rows: "
                + "; ".join(
                    f"{label}={value_text}"
                    for _, label, value_text in preview
                )
            )
        except Exception as exc:
            return f"Could not query web table: {compact_error(exc)}"


class MlbStatsTool(Tool):
    name = "mlb_stats"
    description = (
        "Queries the official MLB Stats API without an API key and sorts a "
        "team's season hitting statistics locally. Use it for historical or "
        "current MLB questions involving walks, at-bats, hits, home runs, RBI, "
        "games, runs, stolen bases, strikeouts, average, OBP, SLG, or OPS."
    )
    inputs = {
        "team": {
            "type": "string",
            "description": "Team name, city, nickname, or abbreviation.",
        },
        "season": {
            "type": "integer",
            "description": "Four-digit MLB season.",
        },
        "sort_stat": {
            "type": "string",
            "description": "Statistic to rank, such as walks, at bats, hits, or home runs.",
        },
    }
    output_type = "string"

    def forward(self, team: str, season: int, sort_stat: str) -> str:
        aliases = {
            "walk": "baseOnBalls",
            "walks": "baseOnBalls",
            "bb": "baseOnBalls",
            "at bat": "atBats",
            "at bats": "atBats",
            "ab": "atBats",
            "hit": "hits",
            "hits": "hits",
            "home run": "homeRuns",
            "home runs": "homeRuns",
            "hr": "homeRuns",
            "rbi": "rbi",
            "games": "gamesPlayed",
            "runs": "runs",
            "stolen bases": "stolenBases",
            "strikeouts": "strikeOuts",
            "average": "avg",
            "avg": "avg",
            "obp": "obp",
            "slg": "slg",
            "ops": "ops",
        }
        requested = " ".join(str(sort_stat or "").lower().split())
        stat_field = aliases.get(requested)
        if not stat_field:
            return "Unsupported MLB statistic: " + requested

        try:
            season = int(season)
            teams_response = requests.get(
                "https://statsapi.mlb.com/api/v1/teams",
                params={"sportId": 1, "season": season},
                timeout=(WEBPAGE_CONNECT_TIMEOUT, WEBPAGE_READ_TIMEOUT),
            )
            teams_response.raise_for_status()
            query = re.sub(r"[^a-z0-9]", "", str(team).lower())
            scored_teams = []
            for item in teams_response.json().get("teams", []):
                names = [
                    item.get("name"),
                    item.get("teamName"),
                    item.get("clubName"),
                    item.get("locationName"),
                    item.get("abbreviation"),
                ]
                normalized = [
                    re.sub(r"[^a-z0-9]", "", str(value).lower())
                    for value in names
                    if value
                ]
                score = max(
                    (
                        3 if query == value else
                        2 if query and query in value else
                        1 if value and value in query else 0
                    )
                    for value in normalized
                )
                if score:
                    scored_teams.append((score, item))
            if not scored_teams:
                return f"No MLB team matched '{team}' in {season}."
            selected = max(scored_teams, key=lambda item: item[0])[1]

            stats_response = requests.get(
                "https://statsapi.mlb.com/api/v1/stats",
                params={
                    "stats": "season",
                    "group": "hitting",
                    "season": season,
                    "teamId": selected["id"],
                    "playerPool": "ALL",
                    "limit": 200,
                },
                timeout=(WEBPAGE_CONNECT_TIMEOUT, WEBPAGE_READ_TIMEOUT),
            )
            stats_response.raise_for_status()
            stats_groups = stats_response.json().get("stats", [])
            splits = stats_groups[0].get("splits", []) if stats_groups else []
            ranked = [
                split
                for split in splits
                if split.get("stat", {}).get(stat_field) not in (None, "")
            ]
            ranked.sort(
                key=lambda split: float(
                    str(split["stat"][stat_field]).replace(",", "")
                ),
                reverse=True,
            )
            ranked = ranked[:10]
            person_ids = ",".join(
                str(split.get("player", {}).get("id"))
                for split in ranked
                if split.get("player", {}).get("id")
            )
            names = {}
            if person_ids:
                people_response = requests.get(
                    "https://statsapi.mlb.com/api/v1/people",
                    params={"personIds": person_ids},
                    timeout=(WEBPAGE_CONNECT_TIMEOUT, WEBPAGE_READ_TIMEOUT),
                )
                people_response.raise_for_status()
                names = {
                    person["id"]: person.get("fullName", str(person["id"]))
                    for person in people_response.json().get("people", [])
                }

            rows = []
            for split in ranked:
                player_id = split.get("player", {}).get("id")
                stat = split.get("stat", {})
                rows.append(
                    f"{names.get(player_id, player_id)}: "
                    f"{stat_field}={stat.get(stat_field)}, "
                    f"atBats={stat.get('atBats')}, "
                    f"baseOnBalls={stat.get('baseOnBalls')}"
                )
            return (
                f"Official MLB season stats — {selected['name']} {season}, "
                f"ranked by {stat_field}:\n" + "\n".join(rows)
            )
        except Exception as exc:
            return f"MLB Stats API query failed: {compact_error(exc)}"


class TranscribeGaiaAudioTool(Tool):
    name = "transcribe_gaia_audio"
    description = (
        "Downloads and transcribes the official GAIA audio attachment. Use it "
        "first whenever the attachment is MP3, WAV, FLAC, M4A, OGG, or WEBM. "
        "It uses Hugging Face speech recognition, not the chat model."
    )
    inputs = {
        "task_id": {
            "type": "string",
            "description": "Exact GAIA task_id associated with the audio.",
        }
    }
    output_type = "string"

    def forward(self, task_id: str) -> str:
        task_id = str(task_id or "").strip()
        try:
            data, filename = download_gaia_attachment(task_id)
            suffix = Path(filename).suffix.lower()
            if suffix not in {
                ".mp3", ".wav", ".flac", ".m4a", ".ogg", ".webm", ".mp4"
            }:
                return (
                    f"Attachment {filename} is not an audio file. Use "
                    "inspect_gaia_attachment."
                )

            hf_token = os.getenv("HF_TOKEN")
            if not hf_token:
                return "Audio transcription failed: HF_TOKEN is not configured."
            try:
                from huggingface_hub import InferenceClient

                client = InferenceClient(api_key=hf_token, provider="auto")
                transcript_result = client.automatic_speech_recognition(
                    data,
                    model=os.getenv(
                        "GAIA_ASR_MODEL", "openai/whisper-large-v3"
                    ),
                )
                transcript = str(
                    getattr(transcript_result, "text", transcript_result)
                ).strip()
                if not transcript:
                    return "Hugging Face returned an empty audio transcript."
                return (
                    f"Audio transcript ({filename}):\n"
                    f"{transcript[:6_000]}"
                )
            except Exception as exc:
                return (
                    "Hugging Face speech-to-text failed: "
                    f"{compact_error(exc)}"
                )
        except Exception as exc:
            return (
                f"Could not transcribe audio for task {task_id}: "
                f"{compact_error(exc)}"
            )


class CalculatorTool(Tool):
    name = "calculator"
    description = (
        "Evaluates arithmetic locally without an LLM. Supports +, -, *, /, //, "
        "**, %, parentheses, pi, e, sqrt, log, exp, sin, cos, tan, abs, and round."
    )
    inputs = {
        "expression": {
            "type": "string",
            "description": "Arithmetic expression to evaluate.",
        }
    }
    output_type = "string"

    def forward(self, expression: str) -> str:
        binary_ops = {
            ast.Add: operator.add,
            ast.Sub: operator.sub,
            ast.Mult: operator.mul,
            ast.Div: operator.truediv,
            ast.FloorDiv: operator.floordiv,
            ast.Mod: operator.mod,
            ast.Pow: operator.pow,
        }
        unary_ops = {ast.UAdd: operator.pos, ast.USub: operator.neg}
        functions = {
            "abs": abs,
            "round": round,
            "sqrt": math.sqrt,
            "log": math.log,
            "exp": math.exp,
            "sin": math.sin,
            "cos": math.cos,
            "tan": math.tan,
        }
        constants = {"pi": math.pi, "e": math.e}

        def evaluate(node):
            if isinstance(node, ast.Expression):
                return evaluate(node.body)
            if isinstance(node, ast.Constant) and isinstance(
                node.value, (int, float)
            ):
                return node.value
            if isinstance(node, ast.BinOp) and type(node.op) in binary_ops:
                return binary_ops[type(node.op)](
                    evaluate(node.left), evaluate(node.right)
                )
            if isinstance(node, ast.UnaryOp) and type(node.op) in unary_ops:
                return unary_ops[type(node.op)](evaluate(node.operand))
            if isinstance(node, ast.Name) and node.id in constants:
                return constants[node.id]
            if (
                isinstance(node, ast.Call)
                and isinstance(node.func, ast.Name)
                and node.func.id in functions
                and not node.keywords
            ):
                return functions[node.func.id](
                    *(evaluate(argument) for argument in node.args)
                )
            raise ValueError("Unsupported expression.")

        try:
            parsed = ast.parse(str(expression), mode="eval")
            result = evaluate(parsed)
            return str(result)
        except Exception as exc:
            return f"Calculation failed: {compact_error(exc)}"


class QueryGaiaSpreadsheetTool(Tool):
    name = "query_gaia_spreadsheet"
    description = (
        "Analyzes an attached XLSX, XLSM, CSV, or TSV locally. Use operation "
        "'describe' first, then sum, mean, min, max, count, unique, or rows. "
        "This avoids sending the entire spreadsheet to the language model."
    )
    inputs = {
        "task_id": {
            "type": "string",
            "description": "Exact GAIA task_id for the spreadsheet.",
        },
        "operation": {
            "type": "string",
            "description": "describe, sum, mean, min, max, count, unique, or rows.",
        },
        "sheet": {
            "type": "string",
            "description": "Sheet name, or an empty string for the first sheet.",
        },
        "column": {
            "type": "string",
            "description": "Target column, or empty for describe/count rows.",
        },
        "filters": {
            "type": "string",
            "description": (
                "Optional exact filters as column=value;column=value. "
                "Use an empty string for no filter."
            ),
        },
    }
    output_type = "string"

    def forward(
        self,
        task_id: str,
        operation: str,
        sheet: str,
        column: str,
        filters: str,
    ) -> str:
        try:
            data, filename = download_gaia_attachment(str(task_id).strip())
            suffix = Path(filename).suffix.lower()
            if suffix in {".xlsx", ".xlsm"}:
                tables = pd.read_excel(BytesIO(data), sheet_name=None)
            elif suffix in {".csv", ".tsv"}:
                separator = "\t" if suffix == ".tsv" else ","
                tables = {
                    "data": pd.read_csv(
                        BytesIO(data),
                        sep=separator,
                        encoding_errors="replace",
                    )
                }
            else:
                return f"Attachment {filename} is not a supported spreadsheet."

            requested_sheet = str(sheet or "").strip()
            sheet_name = next(iter(tables))
            if requested_sheet:
                matching_sheet = next(
                    (
                        name
                        for name in tables
                        if str(name).lower() == requested_sheet.lower()
                    ),
                    None,
                )
                if matching_sheet is None:
                    return (
                        f"Unknown sheet {requested_sheet}. Available: "
                        + ", ".join(map(str, tables))
                    )
                sheet_name = matching_sheet

            frame = tables[sheet_name].copy()
            frame.columns = [str(value).strip() for value in frame.columns]
            requested_column = str(column or "").strip()

            for filter_expression in str(filters or "").split(";"):
                filter_expression = filter_expression.strip()
                if not filter_expression:
                    continue
                if "=" not in filter_expression:
                    return f"Invalid filter: {filter_expression}"
                filter_column, filter_value = (
                    part.strip() for part in filter_expression.split("=", 1)
                )
                actual_filter_column = next(
                    (
                        name
                        for name in frame.columns
                        if name.lower() == filter_column.lower()
                    ),
                    None,
                )
                if actual_filter_column is None:
                    return (
                        f"Unknown filter column {filter_column}. Columns: "
                        + ", ".join(frame.columns)
                    )
                numeric_value = pd.to_numeric(
                    pd.Series([filter_value]), errors="coerce"
                ).iloc[0]
                numeric_column = pd.to_numeric(
                    frame[actual_filter_column], errors="coerce"
                )
                if pd.notna(numeric_value) and numeric_column.notna().any():
                    frame = frame[numeric_column == numeric_value]
                else:
                    frame = frame[
                        frame[actual_filter_column]
                        .astype(str)
                        .str.strip()
                        .str.casefold()
                        == filter_value.casefold()
                    ]

            operation = str(operation or "describe").strip().lower()
            if operation == "describe":
                preview = frame.head(5).to_csv(index=False)
                return (
                    f"Workbook: {filename}\n"
                    f"Sheets: {', '.join(map(str, tables))}\n"
                    f"Selected sheet: {sheet_name}\n"
                    f"Rows: {len(frame)}; Columns: {len(frame.columns)}\n"
                    f"Column names: {', '.join(frame.columns)}\n"
                    f"First rows:\n{preview[:2_500]}"
                )

            if requested_column:
                actual_column = next(
                    (
                        name
                        for name in frame.columns
                        if name.lower() == requested_column.lower()
                    ),
                    None,
                )
                if actual_column is None:
                    return (
                        f"Unknown target column {requested_column}. Columns: "
                        + ", ".join(frame.columns)
                    )
            else:
                actual_column = ""

            if operation == "count":
                result = (
                    int(frame[actual_column].notna().sum())
                    if actual_column
                    else len(frame)
                )
            elif operation in {"sum", "mean", "min", "max"}:
                if not actual_column:
                    return f"Operation {operation} requires a target column."
                series = pd.to_numeric(frame[actual_column], errors="coerce").dropna()
                if series.empty:
                    return f"Column {actual_column} has no numeric values."
                result = getattr(series, operation)()
            elif operation == "unique":
                if not actual_column:
                    return "Operation unique requires a target column."
                values = frame[actual_column].dropna().astype(str).unique().tolist()
                return ", ".join(values[:100])
            elif operation == "rows":
                columns = [actual_column] if actual_column else list(frame.columns)
                return frame[columns].head(30).to_csv(index=False)[:4_000]
            else:
                return (
                    "Unknown operation. Use describe, sum, mean, min, max, "
                    "count, unique, or rows."
                )

            if hasattr(result, "item"):
                result = result.item()
            return (
                f"Operation: {operation}; sheet: {sheet_name}; "
                f"rows matched: {len(frame)}; result: {result}"
            )
        except Exception as exc:
            return f"Spreadsheet analysis failed: {compact_error(exc)}"


class InspectGaiaAttachmentTool(Tool):
    name = "inspect_gaia_attachment"
    description = (
        "Downloads and reads the official attachment associated with a GAIA "
        "task. Use it for PDF, DOCX, text, code, or ZIP files. Do not use it "
        "for audio, images, or spreadsheets; those have specialized tools."
    )
    inputs = {
        "task_id": {
            "type": "string",
            "description": "The exact GAIA task_id supplied in the user task.",
        },
        "query": {
            "type": "string",
            "description": "Short terms for the exact fact to extract.",
        },
    }
    output_type = "string"

    def forward(self, task_id: str, query: str) -> str:
        task_id = str(task_id).strip()
        if not task_id:
            return "No task_id was supplied."

        try:
            data, filename = download_gaia_attachment(task_id)
            suffix = Path(filename).suffix.lower()
            if suffix in {
                ".mp3", ".wav", ".flac", ".m4a", ".ogg", ".webm", ".mp4"
            }:
                return (
                    f"Attachment {filename} is audio. Call "
                    "transcribe_gaia_audio with this task_id."
                )
            if suffix in {".png", ".jpg", ".jpeg", ".webp", ".gif"}:
                return (
                    f"Attachment {filename} is an image. Call "
                    "analyze_gaia_image with this task_id and question."
                )
            if suffix in {".xlsx", ".xlsm", ".csv", ".tsv"}:
                return (
                    f"Attachment {filename} is tabular. Call "
                    "query_gaia_spreadsheet with operation='describe' first."
                )
            extracted = extract_attachment_text(
                data, filename, query=str(query or "")
            )
            return f"Attachment filename: {filename}\n\n{extracted}"
        except Exception as exc:
            return (
                f"Could not inspect attachment for task {task_id}: "
                f"{compact_error(exc)}"
            )


class YouTubeTranscriptTool(Tool):
    name = "youtube_transcript"
    description = (
        "Retrieves the spoken transcript or subtitles of a YouTube video. "
        "Use it for questions asking what a person says in a linked video. "
        "It cannot determine purely visual events."
    )
    inputs = {
        "url": {
            "type": "string",
            "description": "Full YouTube URL or the 11-character video ID.",
        },
        "query": {
            "type": "string",
            "description": "Short terms describing the spoken fact or quote.",
        },
    }
    output_type = "string"

    def forward(self, url: str, query: str) -> str:
        from youtube_transcript_api import YouTubeTranscriptApi

        value = str(url or "").strip()
        match = re.search(
            r"(?:v=|youtu\.be/|shorts/)([A-Za-z0-9_-]{11})", value
        )
        video_id = match.group(1) if match else value
        if not re.fullmatch(r"[A-Za-z0-9_-]{11}", video_id):
            return "Could not identify a valid YouTube video ID."

        try:
            api = YouTubeTranscriptApi()
            transcript = api.fetch(video_id)
            lines = []
            for snippet in transcript:
                text = getattr(snippet, "text", None)
                if text is None and isinstance(snippet, dict):
                    text = snippet.get("text")
                if text:
                    lines.append(str(text))
            result = " ".join(lines).strip()
            return (
                focus_text(result, str(query or ""), 5_000)
                if result
                else "The video has no available transcript."
            )
        except Exception as exc:
            return (
                "Could not retrieve YouTube transcript: "
                f"{compact_error(exc)}"
            )


class AnalyzeGaiaImageTool(Tool):
    name = "analyze_gaia_image"
    description = (
        "Downloads the official image for a GAIA task and analyzes it with a "
        "vision model. Use this for questions whose answer depends on image "
        "pixels, diagrams, chess positions, or visual details."
    )
    inputs = {
        "task_id": {
            "type": "string",
            "description": "The exact GAIA task_id associated with the image.",
        },
        "question": {
            "type": "string",
            "description": "The complete question the image must answer.",
        },
    }
    output_type = "string"

    def forward(self, task_id: str, question: str) -> str:
        token = os.getenv("HF_TOKEN")
        if not token:
            return "Image analysis failed: HF_TOKEN is not configured."

        try:
            data, filename = download_gaia_attachment(str(task_id).strip())
            suffix = Path(filename).suffix.lower()
            mime = {
                ".png": "image/png",
                ".jpg": "image/jpeg",
                ".jpeg": "image/jpeg",
                ".webp": "image/webp",
                ".gif": "image/gif",
            }.get(suffix, "image/png")
            encoded = base64.b64encode(data).decode("ascii")

            vision_model = os.getenv(
                "GAIA_VISION_MODEL",
                "Qwen/Qwen3-VL-235B-A22B-Instruct:cheapest",
            )
            response = requests.post(
                "https://router.huggingface.co/v1/chat/completions",
                headers={
                    "Authorization": f"Bearer {token}",
                    "Content-Type": "application/json",
                },
                json={
                    "model": vision_model,
                    "messages": [
                        {
                            "role": "user",
                            "content": [
                                {
                                    "type": "text",
                                    "text": (
                                        "Analyze the supplied image carefully and "
                                        "answer this task. Return the likely exact "
                                        "answer plus at most one short evidence "
                                        "sentence:\n"
                                        f"{question}"
                                    ),
                                },
                                {
                                    "type": "image_url",
                                    "image_url": {
                                        "url": f"data:{mime};base64,{encoded}"
                                    },
                                },
                            ],
                        }
                    ],
                    "temperature": 0,
                    "max_tokens": 300,
                },
                timeout=HTTP_TIMEOUT,
            )
            response.raise_for_status()
            payload = response.json()
            return str(payload["choices"][0]["message"]["content"]).strip()
        except Exception as exc:
            return (
                "Could not analyze the GAIA image: "
                f"{compact_error(exc)}"
            )


class BasicAgent:
    def __init__(self):
        print("Inicializando o agente GAIA...")

        hf_token = os.getenv("HF_TOKEN")
        cerebras_api_key = os.getenv("CEREBRAS_API_KEY")
        configured_model = os.getenv("GAIA_MODEL_ID")

        model_id = configured_model or DEFAULT_MAIN_MODEL
        if not model_id.lower().startswith("cerebras/"):
            print(
                "GAIA_MODEL_ID não apontava para um modelo Cerebras "
                "e foi ignorado. "
                f"Usando {DEFAULT_MAIN_MODEL}."
            )
            model_id = DEFAULT_MAIN_MODEL
        if not cerebras_api_key:
            raise RuntimeError(
                "O secret CEREBRAS_API_KEY não está configurado. "
                "Adicione a chave em Settings > Variables and secrets > Secrets."
            )

        self.model = LiteLLMModel(
            model_id=model_id,
            api_key=cerebras_api_key,
            temperature=0,
            max_tokens=1_000,
            requests_per_minute=8,
        )
        self.hf_token = hf_token
        self.model_id = model_id
        print(f"Modelo principal selecionado: {model_id}")

        web_search_tool = ConciseWebSearchTool()
        visit_page_tool = OpenWebPageTool()
        wikipedia_tool = WikipediaSearchTool(
            user_agent="GAIA-Course-Agent/1.0 (educational project)",
            language="en",
        )
        wikipedia_tool.description = (
            "Searches English Wikipedia content directly. Use it for questions "
            "that explicitly mention Wikipedia or for encyclopedic facts. "
            "For version-specific or nomination details, verify the exact page "
            "or archive with visit_webpage."
        )

        agent_tools = [
            web_search_tool,
            visit_page_tool,
            QueryWebTableTool(),
            MlbStatsTool(),
            ReadDocumentUrlTool(),
            wikipedia_tool,
            InspectGaiaAttachmentTool(),
            QueryGaiaSpreadsheetTool(),
            TranscribeGaiaAudioTool(),
            YouTubeTranscriptTool(),
            AnalyzeGaiaImageTool(),
            CalculatorTool(),
        ]
        agent_tools = [instrument_tool(tool) for tool in agent_tools]
        self.tools_by_name = {tool.name: tool for tool in agent_tools}

        # GLM returns native tool calls. ToolCallingAgent handles that
        # structured format without parsing generated Python code.
        self.agent = ToolCallingAgent(
            tools=agent_tools,
            model=self.model,
            max_steps=6,
            max_tool_threads=1,
            planning_interval=None,
            description=(
                "Agent designed to solve GAIA benchmark questions with "
                "exact-match answers."
            ),
        )

        exact_match_prompt = """
You are an expert AI assistant solving tasks from the GAIA benchmark.

TOOL ROUTING POLICY:
1. web_search discovers URLs and snippets. It does not read full pages.
2. visit_webpage reads focused passages from HTML; always provide short query
   terms. query_web_table computes min/max from an HTML table locally; use it
   instead of reading a long statistics table. read_document_url reads linked
   PDFs or documents by query. mlb_stats queries official baseball season data.
3. transcribe_gaia_audio is the only tool for attached audio. Call it first
   for MP3, WAV, FLAC, M4A, OGG, WEBM, or MP4.
4. inspect_gaia_attachment handles attached PDF, DOCX, text, code, or ZIP.
   Pass short target terms. For XLSX/CSV use
   query_gaia_spreadsheet instead: describe columns, then run one calculation.
   Never send a whole table to the model.
5. analyze_gaia_image handles attached images. youtube_transcript handles
   spoken YouTube content. calculator evaluates arithmetic locally.
6. wikipedia_search is for Wikipedia or encyclopedic facts. Verify historical,
   nomination, revision, or archive details with an exact webpage.

Research carefully, prefer primary or official sources, and cross-check
uncertain facts. A search snippet alone is insufficient when the source page
can be opened. Never invent a tool, use subprocess, or use shell commands.
When CONTROLLER-PRECOLLECTED EVIDENCE is present, use it first and do not
repeat its exact tool call. If it directly answers the task, immediately call
final_answer instead of researching again.
Do not repeat nearly identical searches. Stop as soon as primary evidence
answers the exact question. Never pass an entire task as a webpage query; use
only names, identifiers, and the target fact. Use at most four tool calls, then
call final_answer with the best supported result.

FINAL RESPONSE POLICY:
Call the final_answer tool with only the requested value. Never write the
answer as plain text instead of calling final_answer. Never
include reasoning, explanations, labels, Markdown, citations, or the words
"FINAL ANSWER" inside the submitted value.
- Quantity/count: return only the number, unless units or currency are requested.
- Person: return only the requested name component.
- City/country/code: return only that value.
- List: return only items with the requested separator and ordering.
- Chess move: return only algebraic notation.
- Quote: return only the requested spoken words.
"""
        self.agent.prompt_templates["system_prompt"] = (
            exact_match_prompt.strip()
            + "\n\n"
            + self.agent.prompt_templates["system_prompt"]
        )

        # Cerebras is primary. Gemini is a real failover agent as well as an
        # optional final reviewer, so a provider billing/quota error does not
        # automatically discard the current question.
        self.gemini_fallback_agent = None
        gemini_api_key = os.getenv("GEMINI_API_KEY")
        if gemini_api_key:
            fallback_name = os.getenv(
                "GAIA_GEMINI_FALLBACK_MODEL", DEFAULT_GEMINI_REVIEW_MODEL
            )
            if not fallback_name.lower().startswith("gemini-"):
                fallback_name = DEFAULT_GEMINI_REVIEW_MODEL
            fallback_model = LiteLLMModel(
                model_id=f"gemini/{fallback_name}",
                api_key=gemini_api_key,
                max_tokens=1_200,
                reasoning_effort="low",
                requests_per_minute=8,
            )
            self.gemini_fallback_agent = ToolCallingAgent(
                tools=agent_tools,
                model=fallback_model,
                max_steps=6,
                max_tool_threads=1,
                planning_interval=None,
                description="Gemini fallback agent for GAIA exact-match tasks.",
            )
            self.gemini_fallback_agent.prompt_templates["system_prompt"] = (
                exact_match_prompt.strip()
                + "\n\n"
                + self.gemini_fallback_agent.prompt_templates["system_prompt"]
            )
            print(f"Fallback Gemini habilitado: {fallback_name}")

    def _precollect_deterministic_evidence(
        self,
        question: str,
        task_id: str | None,
        attachment_name: str = "",
    ) -> str:
        """Runs mandatory/specialized tools by rule before an LLM can choose."""
        suffix = Path(attachment_name).suffix.lower()
        query = concise_query_from_question(question)
        route = ""

        if task_id and suffix in {
            ".mp3", ".wav", ".flac", ".m4a", ".ogg", ".webm", ".mp4"
        }:
            self.tools_by_name["transcribe_gaia_audio"].forward(task_id=task_id)
            route = "audio attachment -> transcribe_gaia_audio"
        elif task_id and suffix in {
            ".png", ".jpg", ".jpeg", ".webp", ".gif"
        }:
            self.tools_by_name["analyze_gaia_image"].forward(
                task_id=task_id,
                question=question,
            )
            route = "image attachment -> analyze_gaia_image"
        elif task_id and suffix in {".xlsx", ".xlsm", ".csv", ".tsv"}:
            self.tools_by_name["query_gaia_spreadsheet"].forward(
                task_id=task_id,
                operation="describe",
                sheet="",
                column="",
                filters="",
            )
            route = "spreadsheet attachment -> query_gaia_spreadsheet(describe)"
        elif task_id and attachment_name:
            self.tools_by_name["inspect_gaia_attachment"].forward(
                task_id=task_id,
                query=query,
            )
            route = "document attachment -> inspect_gaia_attachment"

        youtube_match = re.search(
            r"https?://(?:www\.)?(?:youtube\.com/watch\?v=|youtu\.be/)"
            r"[A-Za-z0-9_-]{11}",
            question,
            flags=re.I,
        )
        spoken_cues = (
            "say", "said", "says", "speak", "spoken", "quote", "transcript",
            "according to the video", "what does", "what did",
        )
        if (
            not route
            and youtube_match
            and any(cue in question.lower() for cue in spoken_cues)
        ):
            self.tools_by_name["youtube_transcript"].forward(
                url=youtube_match.group(0),
                query=query,
            )
            route = "spoken YouTube question -> youtube_transcript"

        team_aliases = {
            "yankee": "Yankees", "red sox": "Red Sox",
            "oriole": "Orioles", "ray": "Rays", "blue jay": "Blue Jays",
            "white sox": "White Sox", "guardian": "Guardians",
            "cleveland indian": "Indians", "tiger": "Tigers",
            "royal": "Royals", "twin": "Twins", "astro": "Astros",
            "angel": "Angels", "athletic": "Athletics", "mariner": "Mariners",
            "ranger": "Rangers", "brave": "Braves", "marlin": "Marlins",
            "met": "Mets", "phillie": "Phillies", "national": "Nationals",
            "cub": "Cubs", "red": "Reds", "brewer": "Brewers",
            "pirate": "Pirates", "cardinal": "Cardinals",
            "diamondback": "Diamondbacks", "rockie": "Rockies",
            "dodger": "Dodgers", "padre": "Padres", "giant": "Giants",
        }
        lower_question = question.lower()
        years = re.findall(r"\b(?:18|19|20)\d{2}\b", question)
        matched_team = next(
            (
                canonical
                for alias, canonical in team_aliases.items()
                if re.search(rf"\b{re.escape(alias)}s?\b", lower_question)
            ),
            None,
        )
        stat_cues = [
            ("most walks", "walks"), ("least walks", "walks"),
            ("walks", "walks"), ("at bats", "at bats"),
            ("home runs", "home runs"), ("stolen bases", "stolen bases"),
            ("strikeouts", "strikeouts"), ("hits", "hits"),
            ("runs batted", "rbi"), ("rbi", "rbi"), ("ops", "ops"),
            ("obp", "obp"), ("slugging", "slg"), ("average", "average"),
        ]
        matched_stat = next(
            (stat for cue, stat in stat_cues if cue in lower_question),
            None,
        )
        if not route and matched_team and years and matched_stat:
            self.tools_by_name["mlb_stats"].forward(
                team=matched_team,
                season=int(years[0]),
                sort_stat=matched_stat,
            )
            route = "MLB statistics question -> official MLB Stats API"

        if route:
            print(f"Rota determinística: {route}")
        return route

    @staticmethod
    def _invalid_candidate(candidate: str) -> bool:
        value = str(candidate or "").strip()
        lowered = value.lower()
        if not value or lowered in {"none", "null", "n/a"}:
            return True
        if len(value) > 1_000:
            return True
        tool_syntax = (
            bool(re.search(
                r"""["']type["']\s*:\s*["']function["']""",
                value,
                flags=re.I,
            ))
            and bool(re.search(
                r"""["']arguments["']\s*:""",
                value,
                flags=re.I,
            ))
        )
        unfinished_cues = (
            "call: ", "calling tool", "let's search", "lets search",
            "let's do a search", "web_search(", "wikipedia_search(",
            "visit_webpage(", "query_web_table(", "i need to search",
        )
        return tool_syntax or any(cue in lowered for cue in unfinished_cues)

    def _gemini_answer_from_evidence(
        self,
        question: str,
        evidence: str,
        task_id: str | None = None,
    ) -> str:
        """Produces one answer from existing evidence without running tools."""
        gemini_api_key = os.getenv("GEMINI_API_KEY")
        if not gemini_api_key:
            raise RuntimeError(
                "GEMINI_API_KEY não está configurada para o fallback."
            )
        model = os.getenv(
            "GAIA_GEMINI_FALLBACK_MODEL", DEFAULT_GEMINI_REVIEW_MODEL
        )
        if not model.lower().startswith("gemini-"):
            model = DEFAULT_GEMINI_REVIEW_MODEL
        prompt = f"""
Answer this GAIA task using ONLY the collected evidence below. Do not call or
suggest tools and do not perform another search. If the evidence is sufficient,
return only the exact requested value in final_answer. If it is insufficient,
set sufficient_evidence to false and leave final_answer empty.

Task ID: {task_id or "test"}
Question: {question}

COLLECTED EVIDENCE:
{evidence}
""".strip()
        response = requests.post(
            (
                "https://generativelanguage.googleapis.com/v1beta/models/"
                f"{model}:generateContent"
            ),
            headers={
                "x-goog-api-key": gemini_api_key,
                "Content-Type": "application/json",
            },
            json={
                "contents": [
                    {"role": "user", "parts": [{"text": prompt}]}
                ],
                "generationConfig": {
                    "maxOutputTokens": 512,
                    "thinkingConfig": {"thinkingLevel": "minimal"},
                    "responseMimeType": "application/json",
                    "responseJsonSchema": {
                        "type": "object",
                        "properties": {
                            "final_answer": {"type": "string"},
                            "sufficient_evidence": {"type": "boolean"},
                        },
                        "required": [
                            "final_answer",
                            "sufficient_evidence",
                        ],
                        "additionalProperties": False,
                    },
                },
            },
            timeout=(WEBPAGE_CONNECT_TIMEOUT, 45),
        )
        response.raise_for_status()
        payload = response.json()
        parts = payload["candidates"][0]["content"]["parts"]
        content = "".join(
            str(part.get("text") or "") for part in parts
        ).strip()
        data = json.loads(content)
        if not data.get("sufficient_evidence"):
            raise RuntimeError(
                "O Gemini informou que as evidências coletadas ainda são "
                "insuficientes. A resposta não foi salva."
            )
        answer = self.enforce_direct_answer(
            question, str(data.get("final_answer") or "")
        )
        if self._invalid_candidate(answer):
            raise RuntimeError(
                "O fallback Gemini não produziu uma resposta final válida."
            )
        print(f"Gemini respondeu usando as evidências existentes: {answer}")
        return answer

    def _run_with_failover(
        self,
        task_context: str,
        question: str,
        task_id: str | None,
    ):
        try:
            return self.agent.run(task_context, reset=True), False
        except Exception as exc:
            error_text = str(exc).lower()
            retryable_provider_error = any(
                marker in error_text
                for marker in (
                    "payment required",
                    "insufficient credit",
                    "billing",
                    "quota",
                    "rate limit",
                    "429",
                    "authenticationerror",
                    "unauthorized",
                    "401",
                    "service unavailable",
                    "timeout",
                )
            )
            if retryable_provider_error and self.gemini_fallback_agent:
                evidence = current_run_evidence()
                print(
                    "Cerebras indisponível; executando a questão com o "
                    f"fallback Gemini. Motivo: {compact_error(exc)}"
                )
                if evidence:
                    return (
                        self._gemini_answer_from_evidence(
                            question=question,
                            evidence=evidence,
                            task_id=task_id,
                        ),
                        True,
                    )
                return (
                    self.gemini_fallback_agent.run(task_context, reset=True),
                    True,
                )
            if retryable_provider_error:
                raise RuntimeError(
                    "O Cerebras recusou a chamada e o fallback Gemini não "
                    "está disponível. Configure GEMINI_API_KEY ou verifique "
                    f"a cota do Cerebras. Detalhe: {compact_error(exc)}"
                ) from exc
            raise

    def __call__(self, question: str, task_id: str | None = None) -> str:
        question = (question or "").strip()
        if not question:
            raise ValueError("Digite uma pergunta para testar o agente.")
        SEARCH_CACHE.clear()
        reset_run_evidence()
        attachment_name = ""

        if task_id:
            attachment_name = get_task_file_name(task_id)
            attachment_suffix = Path(attachment_name).suffix.lower()
            if attachment_suffix in {
                ".mp3", ".wav", ".flac", ".m4a", ".ogg", ".webm", ".mp4"
            }:
                attachment_context = (
                    f"Official attachment: {attachment_name}. Call "
                    "transcribe_gaia_audio first."
                )
            elif attachment_suffix in {
                ".png", ".jpg", ".jpeg", ".webp", ".gif"
            }:
                attachment_context = (
                    f"Official attachment: {attachment_name}. Call "
                    "analyze_gaia_image with this task_id and question."
                )
            elif attachment_suffix in {".xlsx", ".xlsm", ".csv", ".tsv"}:
                attachment_context = (
                    f"Official attachment: {attachment_name}. Call "
                    "query_gaia_spreadsheet with operation='describe' first."
                )
            elif attachment_name:
                attachment_context = (
                    f"Official attachment: {attachment_name}. Call "
                    "inspect_gaia_attachment with this task_id and short target terms."
                )
            else:
                attachment_context = (
                    "Official attachment: NONE. Do not call any GAIA "
                    "attachment tool."
                )
            task_context = (
                f"GAIA task_id: {task_id}\n"
                f"{attachment_context}\n\nQuestion: {question}"
            )
        else:
            task_context = question
        route = self._precollect_deterministic_evidence(
            question=question,
            task_id=task_id,
            attachment_name=attachment_name,
        )
        precollected = current_run_evidence()
        if precollected:
            task_context += (
                "\n\nCONTROLLER-PRECOLLECTED EVIDENCE:\n"
                f"{precollected}\n\n"
                f"Controller route: {route}. Do not repeat the same tool call; "
                "a new spreadsheet calculation is allowed after describe. "
                "If this evidence answers the question, call final_answer now."
            )
        try:
            result, used_gemini_fallback = self._run_with_failover(
                task_context=task_context,
                question=question,
                task_id=task_id,
            )
        except Exception as exc:
            error_text = str(exc)
            if (
                "AuthenticationError" in error_text
                or "401" in error_text
                or "Unauthorized" in error_text
            ):
                raise RuntimeError(
                    f"Falha de autenticação no modelo {self.model_id}. "
                    "Verifique se HF_TOKEN possui permissão para usar "
                    "Inference Providers."
                ) from exc
            raise
        candidate = self.enforce_direct_answer(question, str(result))
        invalid_candidate = (
            not candidate
            or candidate.strip().lower() in {"none", "null"}
            or (
                bool(re.search(
                    r"""["']type["']\s*:\s*["']function["']""",
                    candidate,
                    flags=re.I,
                ))
                and bool(re.search(
                    r"""["']arguments["']\s*:""",
                    candidate,
                    flags=re.I,
                ))
            )
        )
        invalid_candidate = (
            invalid_candidate or self._invalid_candidate(candidate)
        )
        if (
            invalid_candidate
            and self.gemini_fallback_agent
            and not used_gemini_fallback
        ):
            print(
                "Cerebras terminou sem resposta final; repetindo a questão "
                "com o fallback Gemini."
            )
            evidence = current_run_evidence()
            used_gemini_fallback = True
            if evidence:
                candidate = self._gemini_answer_from_evidence(
                    question=question,
                    evidence=evidence,
                    task_id=task_id,
                )
            else:
                result = self.gemini_fallback_agent.run(
                    task_context, reset=True
                )
                candidate = self.enforce_direct_answer(
                    question, str(result)
                )
            invalid_candidate = (
                not candidate
                or candidate.strip().lower() in {"none", "null"}
                or (
                    bool(re.search(
                        r"""["']type["']\s*:\s*["']function["']""",
                        candidate,
                        flags=re.I,
                    ))
                    and bool(re.search(
                        r"""["']arguments["']\s*:""",
                        candidate,
                        flags=re.I,
                    ))
                )
            )
            invalid_candidate = (
                invalid_candidate or self._invalid_candidate(candidate)
            )
        if invalid_candidate:
            raise RuntimeError(
                "O agente esgotou as etapas sem produzir uma resposta final. "
                "A resposta não foi salva; execute novamente esta questão."
            )
        if used_gemini_fallback:
            print(f"Resposta final produzida pelo fallback Gemini: {candidate}")
            return candidate
        return self.review_answer_with_gemini(
            question=question,
            candidate=candidate,
            task_id=task_id,
            evidence=current_run_evidence(),
        )

    @staticmethod
    def deterministic_answer_cleanup(answer: str) -> str:
        """Remove embalagens comuns sem alterar o conteúdo da resposta."""
        text = str(answer or "").strip()
        text = re.sub(r"</?code>", "", text, flags=re.I).strip()
        text = re.sub(r"^```(?:text|markdown)?\s*", "", text, flags=re.I)
        text = re.sub(r"\s*```$", "", text)
        text = re.sub(r"^\s*#{1,6}\s*", "", text)
        text = re.sub(r"^\s*[-*•]\s+", "", text)
        text = re.sub(r"\[([^\]]+)\]\([^)]+\)", r"\1", text)

        final_call = re.search(
            r"final_answer\s*\(\s*([\"']?)(.*?)\1\s*\)\s*$",
            text,
            flags=re.I | re.S,
        )
        if final_call:
            text = final_call.group(2).strip()

        marker_pattern = re.compile(
            r"(?:final\s+answer|answer|resposta\s+final|resposta)\s*:\s*",
            flags=re.I,
        )
        marker_matches = list(marker_pattern.finditer(text))
        if marker_matches:
            text = text[marker_matches[-1].end() :].strip()

        prefix_patterns = [
            r"^the\s+(?:final\s+)?answer\s+is\s+",
            r"^my\s+(?:final\s+)?answer\s+is\s+",
            r"^a\s+resposta(?:\s+final)?\s+[ée]\s+",
        ]
        for pattern in prefix_patterns:
            text = re.sub(pattern, "", text, flags=re.I).strip()

        text = text.replace("**", "").replace("__", "").strip()
        if (
            len(text) >= 2
            and text[0] == text[-1]
            and text[0] in {'"', "'", "`"}
        ):
            text = text[1:-1].strip()

        return text.replace("FINAL ANSWER", "").strip()

    @classmethod
    def enforce_direct_answer(cls, question: str, answer: str) -> str:
        """Impõe o formato exact-match sem pedir nova interpretação a uma LLM."""
        original_text = str(answer or "")
        bold_values = [
            value.strip()
            for value in re.findall(r"\*\*(.+?)\*\*", original_text, flags=re.S)
            if value.strip()
        ]
        text = cls.deterministic_answer_cleanup(answer)
        question_lower = str(question or "").lower()

        lines = [line.strip() for line in text.splitlines() if line.strip()]
        if len(lines) > 1:
            # Para listas, privilegia a linha que realmente contém os itens.
            if "comma" in question_lower or "vírgula" in question_lower:
                comma_lines = [line for line in lines if "," in line]
                if comma_lines:
                    text = max(comma_lines, key=lambda value: value.count(","))
                else:
                    text = lines[-1]
            else:
                text = lines[-1]

        text = cls.deterministic_answer_cleanup(text)

        if "page numbers" in question_lower or "page number" in question_lower:
            page_groups = re.findall(
                r"\b(?:pages?|pp\.?)\s*(?:are|is|:|-)?\s*"
                r"(\d+(?:(?:\s*,\s*(?:and\s+)?|\s+and\s+|-)\d+)*)",
                text,
                flags=re.I,
            )
            if page_groups:
                pages = [int(value) for value in re.findall(r"\d+", page_groups[-1])]
                if pages:
                    return ", ".join(str(value) for value in sorted(set(pages)))

        quantity_question = (
            "how many" in question_lower
            or "numeric output" in question_lower
            or "quantos" in question_lower
            or "quantas" in question_lower
        )
        if quantity_question:
            numbers = re.findall(
                r"(?<![\w.])-?\d+(?:,\d{3})*(?:\.\d+)?", text
            )
            if numbers:
                return numbers[-1].replace(",", "")

        requests_usd = (
            "in usd" in question_lower
            or "usd with" in question_lower
            or "dollars" in question_lower
        )
        if requests_usd:
            amounts = re.findall(
                r"\$?\s*(-?\d+(?:,\d{3})*(?:\.\d+)?)", text
            )
            if amounts:
                raw_amount = amounts[-1].replace(",", "")
                try:
                    return f"${float(raw_amount):,.2f}"
                except ValueError:
                    pass

        if "award number" in question_lower or "grant number" in question_lower:
            identifiers = re.findall(r"\b[A-Z0-9][A-Z0-9-]{5,}\b", text.upper())
            identifiers = [
                value
                for value in identifiers
                if re.search(r"[A-Z]", value) and re.search(r"\d", value)
            ]
            if identifiers:
                return identifiers[-1].strip(" .,:;\"'")

        list_question = (
            "comma" in question_lower
            or "list" in question_lower
            or "separated" in question_lower
            or "delimited" in question_lower
        )
        if (
            list_question
            and "last name" in question_lower
            and "before" in question_lower
            and "after" in question_lower
        ):
            emphasized_names = [
                cls.deterministic_answer_cleanup(value).strip(" .,:;\"'")
                for value in bold_values
            ]
            emphasized_names = [
                value
                for value in emphasized_names
                if value and len(value) <= 60 and " " not in value
            ]
            if len(emphasized_names) >= 2:
                return ", ".join(emphasized_names[-2:])

            name_pair = re.search(
                r"\b(?:are|were)\s+([A-Z][A-Za-z'’-]+)\s*"
                r"(?:,|and)\s*([A-Z][A-Za-z'’-]+)",
                text,
            )
            if name_pair:
                return f"{name_pair.group(1)}, {name_pair.group(2)}"

        person_question = not list_question and (
            question_lower.startswith("who ")
            or " who " in f" {question_lower} "
            or "first name" in question_lower
            or "surname" in question_lower
            or "username" in question_lower
        )
        if person_question:
            # Explanatory answers often repeat the requested person in the
            # final bold fragment. Prefer it before trying sentence patterns.
            if bold_values:
                emphasized = cls.deterministic_answer_cleanup(bold_values[-1])
                if (
                    emphasized
                    and len(emphasized) <= 100
                    and not re.search(r"[.!?]\s+\w", emphasized)
                ):
                    return emphasized.strip(" .,:;\"'")

            person_patterns = [
                r"\b(?:nominated|written|directed|created|founded|authored|performed)"
                r"\s+by\s+([A-Z][\w'’-]*(?:\s+[A-Z][\w'’-]*){0,3})",
                r"\b(?:username|first\s+name|surname|name)\s+(?:is|was)\s+"
                r"([A-Z][\w'’-]*(?:\s+[A-Z][\w'’-]*){0,3})",
            ]
            matches = []
            for pattern in person_patterns:
                matches.extend(re.findall(pattern, text))
            if matches:
                return matches[-1].strip(" .,:;\"'")

        # Remove frases introdutórias que ainda possam aparecer em uma linha.
        text = re.sub(
            r"^(?:therefore,\s*|thus,\s*|so,\s*)?"
            r"(?:the\s+)?(?:correct\s+|final\s+)?answer\s+is\s+",
            "",
            text,
            flags=re.I,
        ).strip()
        text = re.sub(
            r"^(?:the\s+requested\s+)?"
            r"(?:first\s+name|surname|city|country|ioc\s+code)\s+is\s+",
            "",
            text,
            flags=re.I,
        ).strip()

        # Se ainda restar uma explicação seguida de dois-pontos, conserva o valor.
        if ":" in text:
            prefix, value = text.rsplit(":", 1)
            if len(value.strip()) <= 250 and any(
                cue in prefix.lower()
                for cue in ("answer", "resposta", "result", "resultado")
            ):
                text = value.strip()

        return cls.deterministic_answer_cleanup(text)

    def format_exact_answer(self, question: str, raw_answer: str) -> str:
        """Limpa o resultado mecanicamente, sem pedir a outro modelo para alterá-lo."""
        del question
        cleaned = self.deterministic_answer_cleanup(raw_answer)
        lines = [line.strip() for line in cleaned.splitlines() if line.strip()]

        # Quando o agente ainda inclui uma explicação e deixa uma resposta curta
        # isolada na última linha, conserva somente essa última linha.
        if len(lines) > 1:
            last_line = lines[-1]
            reasoning_cues = (
                "because",
                "therefore",
                "research",
                "source",
                "conclude",
                "analysis",
                "porque",
                "portanto",
                "pesquisa",
                "conclu",
            )
            preceding = " ".join(lines[:-1]).lower()
            if len(last_line) <= 250 and any(
                cue in preceding for cue in reasoning_cues
            ):
                cleaned = last_line

        return self.deterministic_answer_cleanup(cleaned)

    def review_answer_with_gemini(
        self,
        question: str,
        candidate: str,
        task_id: str | None = None,
        evidence: str = "",
    ) -> str:
        """Revisa com Gemini e sempre preserva a resposta primária se falhar."""
        gemini_api_key = os.getenv("GEMINI_API_KEY")
        if not gemini_api_key:
            fallback = self.enforce_direct_answer(question, candidate)
            print("Gemini review status: SKIPPED — GEMINI_API_KEY is missing")
            print(f"Primary answer preserved: {fallback}")
            return fallback

        reviewer_model = os.getenv(
            "GAIA_GEMINI_REVIEW_MODEL", DEFAULT_GEMINI_REVIEW_MODEL
        )
        if not reviewer_model.lower().startswith("gemini-"):
            reviewer_model = DEFAULT_GEMINI_REVIEW_MODEL
        review_prompt = f"""
Review this GAIA exact-match candidate. Preserve it unless a correction is
clearly necessary. The final_answer must contain only the requested value,
without explanation, label, Markdown, or citation. Respect requested numeric,
currency, name, list separator/order, quote, or chess notation formats.
Use only the collected evidence when it is present. Do not invent facts and do
not suggest or call another tool.
Task ID: {task_id or "test"}
Question: {question}
Candidate: {candidate}
Collected evidence:
{evidence or "(none; format-check the candidate only)"}
""".strip()

        try:
            response = requests.post(
                (
                    "https://generativelanguage.googleapis.com/v1beta/models/"
                    f"{reviewer_model}:generateContent"
                ),
                headers={
                    "x-goog-api-key": gemini_api_key,
                    "Content-Type": "application/json",
                },
                json={
                    "contents": [
                        {
                            "role": "user",
                            "parts": [{"text": review_prompt}],
                        }
                    ],
                    "generationConfig": {
                        "maxOutputTokens": 512,
                        "thinkingConfig": {"thinkingLevel": "minimal"},
                        "responseMimeType": "application/json",
                        "responseJsonSchema": {
                            "type": "object",
                            "properties": {
                                "final_answer": {
                                    "type": "string",
                                    "description": (
                                        "Only the exact value requested by the "
                                        "question, with no explanation."
                                    ),
                                },
                                "review_note": {
                                    "type": "string",
                                    "description": "Optional very short audit note.",
                                },
                            },
                            "required": ["final_answer"],
                            "additionalProperties": False,
                        },
                    },
                },
                timeout=(WEBPAGE_CONNECT_TIMEOUT, 45),
            )
            response.raise_for_status()
            payload = response.json()
            parts = payload["candidates"][0]["content"]["parts"]
            content = "".join(
                str(part.get("text") or "") for part in parts
            ).strip()
            review_data = json.loads(content)
            final_answer = self.enforce_direct_answer(
                question, str(review_data.get("final_answer") or "")
            )
            if not final_answer:
                raise ValueError("Gemini returned an empty final_answer.")

            note = str(review_data.get("review_note") or "").strip()
            print(f"Candidate answer: {candidate}")
            print(f"Gemini reviewed answer: {final_answer}")
            print(f"Gemini changed answer: {final_answer != candidate}")
            print(f"Gemini review note: {note}")
            return final_answer
        except Exception as exc:
            fallback = self.enforce_direct_answer(question, candidate)
            if not fallback:
                raise RuntimeError(
                    "Gemini review failed and the primary answer was empty. "
                    f"Detail: {compact_error(exc)}"
                ) from exc
            detail = compact_error(exc)
            if "response" in locals() and response is not None:
                detail += f" Response: {response.text[:500]}"
            print(f"Candidate answer: {fallback}")
            print("Gemini review status: FAILED — primary answer preserved")
            print(f"Gemini review error: {detail}")
            return fallback


def empty_results() -> pd.DataFrame:
    return pd.DataFrame(columns=RESULT_COLUMNS)


def answer_readiness(answer: str) -> tuple[int, str]:
    """Avalia somente se a resposta parece pronta; não verifica o gabarito."""
    text = str(answer or "").strip()
    lowered = text.lower()
    if not text:
        return 0, "resposta vazia"
    if lowered.startswith("error:") or "erro" in lowered[:30]:
        return 0, "erro de execução"
    if lowered in {"not found", "unknown", "não encontrado", "n/a", "none"}:
        return 20, "resposta inconclusiva"
    if "final answer" in lowered:
        return 50, "contém texto proibido"
    if len(text) > 500:
        return 60, "resposta possivelmente longa para exact match"
    return 100, "formato aparentemente pronto"


def readiness_summary(results_table) -> str:
    try:
        if isinstance(results_table, pd.DataFrame):
            dataframe = results_table
        else:
            dataframe = pd.DataFrame(results_table, columns=RESULT_COLUMNS)
        if dataframe.empty:
            return "Índice de prontidão: 0% — nenhuma resposta gerada."

        evaluations = [
            answer_readiness(row.get("Submitted Answer", ""))
            for _, row in dataframe.iterrows()
        ]
        scores = [score for score, _ in evaluations]
        ready = sum(score == 100 for score in scores)
        problems = len(scores) - ready
        average = round(sum(scores) / len(scores))
        return (
            f"Índice de prontidão: {average}% — {ready}/{len(scores)} respostas "
            f"aparentemente prontas; {problems} precisam de revisão. "
            "Este índice avalia erros e formato, não a correção do gabarito. "
            "A nota oficial só existe após o envio."
        )
    except Exception as exc:
        return f"Não foi possível calcular o índice de prontidão: {exc}"


def review_dataframe(questions: list, answers: dict) -> pd.DataFrame:
    rows = []
    for item in questions or []:
        task_id = str(item.get("task_id", "")).strip()
        rows.append(
            {
                "Task ID": task_id,
                "Question": str(item.get("question", "")),
                "Submitted Answer": str((answers or {}).get(task_id, "")),
            }
        )
    return pd.DataFrame(rows, columns=RESULT_COLUMNS)


def progress_summary(questions: list, answers: dict) -> str:
    total = len(questions or [])
    answered = sum(
        bool(str((answers or {}).get(str(item.get("task_id", "")), "")).strip())
        for item in (questions or [])
    )
    return f"Progresso: {answered}/{total} questões respondidas."


def load_evaluation_questions(profile: gr.OAuthProfile | None):
    """Carrega as 20 questões, mas não executa o agente."""
    if not profile:
        return (
            "Faça login no Hugging Face primeiro.",
            [],
            {},
            gr.update(choices=[], value=None),
            "",
            "",
            "",
            "Progresso: 0/20 questões respondidas.",
            "Índice de prontidão: 0%.",
            empty_results(),
        )

    try:
        response = requests.get(
            f"{DEFAULT_API_URL}/questions", timeout=HTTP_TIMEOUT
        )
        response.raise_for_status()
        questions = response.json()
        questions = [
            item
            for item in questions
            if item.get("task_id") and item.get("question") is not None
        ]
        if not questions:
            raise ValueError("A API retornou uma lista vazia.")

        answers = {}
        choices = [
            (
                f"{index + 1:02d}. {str(item['question'])[:90]}",
                str(item["task_id"]),
            )
            for index, item in enumerate(questions)
        ]
        first = questions[0]
        dataframe = review_dataframe(questions, answers)
        return (
            f"{len(questions)} questões carregadas. Nenhuma foi executada ainda.",
            questions,
            answers,
            gr.update(choices=choices, value=str(first["task_id"])),
            str(first["question"]),
            str(first["task_id"]),
            "",
            progress_summary(questions, answers),
            readiness_summary(dataframe),
            dataframe,
        )
    except Exception as exc:
        return (
            f"Erro ao carregar questões: {exc}",
            [],
            {},
            gr.update(choices=[], value=None),
            "",
            "",
            "",
            "Progresso: 0/20 questões respondidas.",
            "Índice de prontidão: 0%.",
            empty_results(),
        )


def select_evaluation_question(task_id: str, questions: list, answers: dict):
    for item in questions or []:
        if str(item.get("task_id")) == str(task_id):
            return (
                str(item.get("question", "")),
                str(item.get("task_id", "")),
                str((answers or {}).get(str(task_id), "")),
                "Questão selecionada. Execute o agente ou edite e salve a resposta.",
            )
    return "", "", "", "Questão não encontrada na sessão."


def save_reviewed_answer(
    task_id: str, answer: str, questions: list, answers: dict
):
    task_id = str(task_id or "").strip()
    if not task_id:
        dataframe = review_dataframe(questions, answers)
        return (
            answers or {},
            "Nenhuma questão selecionada.",
            progress_summary(questions, answers),
            readiness_summary(dataframe),
            dataframe,
        )

    updated = dict(answers or {})
    updated[task_id] = str(answer or "").strip()
    dataframe = review_dataframe(questions, updated)
    return (
        updated,
        "Resposta revisada e salva nesta sessão.",
        progress_summary(questions, updated),
        readiness_summary(dataframe),
        dataframe,
    )


def run_current_evaluation_question(
    question: str, task_id: str, questions: list, answers: dict
):
    """Executa somente a questão atualmente selecionada."""
    if not question or not task_id:
        dataframe = review_dataframe(questions, answers)
        return (
            "",
            answers or {},
            "Carregue e selecione uma questão primeiro.",
            progress_summary(questions, answers),
            readiness_summary(dataframe),
            dataframe,
        )

    try:
        answer = BasicAgent()(question, task_id)
        had_previous_answer = bool(
            str((answers or {}).get(str(task_id), "")).strip()
        )
        updated = dict(answers or {})
        updated[str(task_id)] = answer
        dataframe = review_dataframe(questions, updated)
        action_message = (
            "A resposta anterior foi substituída pela nova resposta."
            if had_previous_answer
            else "A primeira resposta desta questão foi salva."
        )
        return (
            answer,
            updated,
            f"Questão executada. {action_message} "
            "Revise o conteúdo antes de avançar.",
            progress_summary(questions, updated),
            readiness_summary(dataframe),
            dataframe,
        )
    except Exception as exc:
        dataframe = review_dataframe(questions, answers)
        return (
            f"ERROR: {exc}",
            answers or {},
            f"Erro ao executar esta questão: {exc}",
            progress_summary(questions, answers),
            readiness_summary(dataframe),
            dataframe,
        )


def run_all_evaluation_questions(
    profile: gr.OAuthProfile | None, questions: list, answers: dict
):
    """Executa todas as questões carregadas e reúne as respostas para revisão."""
    if not profile:
        dataframe = review_dataframe(questions, answers)
        return (
            answers or {},
            "Faça login no Hugging Face primeiro.",
            progress_summary(questions, answers),
            readiness_summary(dataframe),
            dataframe,
        )

    if not questions:
        dataframe = review_dataframe(questions, answers)
        return (
            answers or {},
            "Primeiro clique em 'Carregar as 20 questões'.",
            progress_summary(questions, answers),
            readiness_summary(dataframe),
            dataframe,
        )

    try:
        agent = BasicAgent()
    except Exception as exc:
        dataframe = review_dataframe(questions, answers)
        return (
            answers or {},
            f"Erro ao inicializar o agente: {exc}",
            progress_summary(questions, answers),
            readiness_summary(dataframe),
            dataframe,
        )

    updated = dict(answers or {})
    failures = 0
    for item in questions:
        task_id = str(item.get("task_id", "")).strip()
        question = str(item.get("question", "")).strip()
        if not task_id or not question:
            continue
        try:
            updated[task_id] = agent(question, task_id)
        except Exception as exc:
            print(
                f"Questão {task_id} falhou e não foi salva: "
                f"{compact_error(exc)}"
            )
            failures += 1

    dataframe = review_dataframe(questions, updated)
    status = (
        f"Execução das {len(questions)} questões concluída. "
        f"Falhas encontradas: {failures}. Revise as respostas antes de enviar."
    )
    return (
        updated,
        status,
        progress_summary(questions, updated),
        readiness_summary(dataframe),
        dataframe,
    )


def fetch_random_question():
    """Busca somente uma questão oficial aleatória, sem executar ou enviar."""
    try:
        response = requests.get(
            f"{DEFAULT_API_URL}/random-question", timeout=HTTP_TIMEOUT
        )
        response.raise_for_status()
        item = response.json()
        task_id = str(item.get("task_id", "")).strip()
        question = str(item.get("question", "")).strip()
        if not task_id or not question:
            raise ValueError("A API retornou uma pergunta em formato inválido.")
        return (
            "Uma questão GAIA foi carregada. Clique em 'Testar esta questão'.",
            question,
            task_id,
            "",
            "",
        )
    except Exception as exc:
        return f"Erro ao buscar questão: {exc}", "", "", "", ""


def test_agent(question: str, task_id: str):
    """Executa uma única pergunta oficial sem enviar a avaliação."""
    if not question or not task_id:
        return "Primeiro carregue uma questão GAIA.", "", ""
    try:
        answer = BasicAgent()(question, task_id)
        score, reason = answer_readiness(answer)
        readiness = (
            f"Índice de prontidão: {score}% ({reason}). "
            "Não é a nota oficial e não compara com o gabarito."
        )
        return "Teste concluído. Nenhum resultado foi enviado.", answer, readiness
    except Exception as exc:
        return f"Erro no teste: {exc}", "", "Índice de prontidão: 0%."


def run_agent_only(profile: gr.OAuthProfile | None):
    """Busca as perguntas e gera uma tabela editável, sem enviar respostas."""
    if not profile:
        return (
            "Faça login no Hugging Face primeiro.",
            empty_results(),
            "Índice de prontidão: 0%.",
        )

    try:
        agent = BasicAgent()
    except Exception as exc:
        return (
            f"Erro ao inicializar o agente: {exc}",
            empty_results(),
            "Índice de prontidão: 0%.",
        )

    questions_url = f"{DEFAULT_API_URL}/questions"
    try:
        response = requests.get(questions_url, timeout=30)
        response.raise_for_status()
        questions = response.json()
        if not questions:
            return (
                "A API retornou uma lista de perguntas vazia.",
                empty_results(),
                "Índice de prontidão: 0%.",
            )
    except Exception as exc:
        return (
            f"Erro ao buscar perguntas: {exc}",
            empty_results(),
            "Índice de prontidão: 0%.",
        )

    results = []
    for item in questions:
        task_id = item.get("task_id")
        question = item.get("question")
        if not task_id or question is None:
            continue

        try:
            answer = agent(question, task_id)
        except Exception as exc:
            print(
                f"Questão {task_id} falhou e foi omitida da avaliação local: "
                f"{compact_error(exc)}"
            )
            continue

        results.append(
            {
                "Task ID": task_id,
                "Question": question,
                "Submitted Answer": answer,
            }
        )

    if not results:
        return (
            "O agente não produziu respostas.",
            empty_results(),
            "Índice de prontidão: 0%.",
        )

    status = (
        f"Execução concluída: {len(results)} respostas geradas. "
        "Revise e, se necessário, edite a coluna 'Submitted Answer'. "
        "Nada foi enviado ainda."
    )
    dataframe = pd.DataFrame(results, columns=RESULT_COLUMNS)
    return status, dataframe, readiness_summary(dataframe)


def normalize_results(results_table) -> list[dict]:
    """Converte a tabela revisada no payload exigido pela API."""
    if results_table is None:
        return []

    if isinstance(results_table, pd.DataFrame):
        dataframe = results_table.copy()
    else:
        dataframe = pd.DataFrame(results_table, columns=RESULT_COLUMNS)

    if dataframe.empty:
        return []

    missing = set(RESULT_COLUMNS) - set(dataframe.columns)
    if missing:
        raise ValueError(
            "A tabela de revisão não contém as colunas esperadas: "
            + ", ".join(sorted(missing))
        )

    answers = []
    for _, row in dataframe.iterrows():
        task_id = str(row["Task ID"]).strip()
        answer = str(row["Submitted Answer"]).strip()
        if not task_id or task_id.lower() == "nan":
            continue
        if not answer or answer.lower() == "nan":
            raise ValueError(f"A tarefa {task_id} está sem resposta.")
        if answer.startswith("ERROR:"):
            raise ValueError(
                f"A tarefa {task_id} ainda contém um erro. "
                "Corrija a resposta antes de enviar."
            )
        answers.append({"task_id": task_id, "submitted_answer": answer})

    return answers


def submit_to_leaderboard(
    profile: gr.OAuthProfile | None, results_table
):
    """Envia exatamente os valores atualmente visíveis na tabela revisada."""
    if not profile:
        return "Faça login no Hugging Face primeiro."

    try:
        answers = normalize_results(results_table)
    except Exception as exc:
        return f"Envio bloqueado: {exc}"

    if not answers:
        return "Não há respostas para enviar. Execute a avaliação primeiro."

    space_id = os.getenv("SPACE_ID")
    if not space_id:
        return (
            "Envio bloqueado: a variável SPACE_ID não foi encontrada. "
            "Publique/execute este app em um Hugging Face Space."
        )

    submission = {
        "username": profile.username.strip(),
        "agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
        "answers": answers,
    }

    try:
        response = requests.post(
            f"{DEFAULT_API_URL}/submit",
            json=submission,
            timeout=90,
        )
        response.raise_for_status()
        result = response.json()
        return (
            "Envio realizado com sucesso!\n"
            f"Usuário: {result.get('username')}\n"
            f"Pontuação: {result.get('score', 'N/A')}% "
            f"({result.get('correct_count', '?')}/"
            f"{result.get('total_attempted', '?')} corretas)\n"
            f"Mensagem: {result.get('message', 'Sem mensagem.')}"
        )
    except requests.exceptions.RequestException as exc:
        detail = ""
        if exc.response is not None:
            detail = f" Resposta da API: {exc.response.text[:500]}"
        return f"Falha no envio: {exc}.{detail}"
    except Exception as exc:
        return f"Erro inesperado no envio: {exc}"


with gr.Blocks(theme=gr.themes.Soft(), title="GAIA Agent Evaluation") as demo:
    gr.Markdown("# GAIA Agent Evaluation")
    gr.Markdown(
        "Teste o agente isoladamente, gere as respostas oficiais para revisão "
        "e só então faça o envio final."
    )
    gr.LoginButton()

    with gr.Tabs():
        with gr.Tab("1. Testar agente"):
            gr.Markdown(
                "Carregue uma única pergunta aleatória da avaliação GAIA e "
                "teste o agente sem enviar nenhuma resposta."
            )
            random_task_id = gr.State("")
            test_question = gr.Textbox(
                label="Pergunta GAIA sorteada",
                lines=5,
                interactive=False,
            )
            with gr.Row():
                fetch_question_button = gr.Button(
                    "Carregar uma pergunta GAIA", variant="secondary"
                )
                test_button = gr.Button(
                    "Testar esta questão", variant="primary"
                )
            test_status = gr.Textbox(label="Status", interactive=False)
            test_answer = gr.Textbox(
                label="Resposta do agente", lines=5, interactive=False
            )
            test_readiness = gr.Textbox(
                label="Avaliação antes do envio",
                lines=3,
                interactive=False,
            )
            fetch_question_button.click(
                fn=fetch_random_question,
                outputs=[
                    test_status,
                    test_question,
                    random_task_id,
                    test_answer,
                    test_readiness,
                ],
            )
            test_button.click(
                fn=test_agent,
                inputs=[test_question, random_task_id],
                outputs=[test_status, test_answer, test_readiness],
            )

        with gr.Tab("2. Executar e revisar"):
            gr.Markdown(
                "Carregue as 20 questões e execute apenas a questão selecionada. "
                "As respostas ficam reunidas para uma única submissão final."
            )
            evaluation_questions = gr.State([])
            evaluation_answers = gr.State({})
            current_task_id = gr.State("")

            with gr.Row():
                load_questions_button = gr.Button(
                    "Carregar as 20 questões", variant="secondary"
                )
                run_all_button = gr.Button(
                    "Executar as 20 questões", variant="primary"
                )
            question_selector = gr.Dropdown(
                label="Escolha a questão",
                choices=[],
                interactive=True,
            )
            current_question = gr.Textbox(
                label="Questão selecionada",
                lines=6,
                interactive=False,
            )
            current_answer = gr.Textbox(
                label="Resposta da questão selecionada",
                lines=4,
                interactive=True,
                placeholder=(
                    "Execute o agente ou digite/corrija a resposta e clique em salvar."
                ),
            )
            with gr.Row():
                run_current_button = gr.Button(
                    "Responder novamente esta questão", variant="primary"
                )
                save_answer_button = gr.Button("Salvar resposta revisada")

            run_status = gr.Textbox(label="Status", lines=4, interactive=False)
            evaluation_progress = gr.Textbox(
                label="Progresso",
                value="Progresso: 0/20 questões respondidas.",
                interactive=False,
            )
            evaluation_readiness = gr.Textbox(
                label="Avaliação antes do envio",
                lines=4,
                interactive=False,
                value="Índice de prontidão: 0% — nenhuma resposta gerada.",
            )
            results_table = gr.DataFrame(
                headers=RESULT_COLUMNS,
                datatype=["str", "str", "str"],
                value=empty_results(),
                label="Respostas para revisão",
                wrap=True,
                interactive=False,
            )

            load_questions_button.click(
                fn=load_evaluation_questions,
                outputs=[
                    run_status,
                    evaluation_questions,
                    evaluation_answers,
                    question_selector,
                    current_question,
                    current_task_id,
                    current_answer,
                    evaluation_progress,
                    evaluation_readiness,
                    results_table,
                ],
            )
            run_all_button.click(
                fn=run_all_evaluation_questions,
                inputs=[evaluation_questions, evaluation_answers],
                outputs=[
                    evaluation_answers,
                    run_status,
                    evaluation_progress,
                    evaluation_readiness,
                    results_table,
                ],
            )
            question_selector.change(
                fn=select_evaluation_question,
                inputs=[
                    question_selector,
                    evaluation_questions,
                    evaluation_answers,
                ],
                outputs=[
                    current_question,
                    current_task_id,
                    current_answer,
                    run_status,
                ],
            )
            run_current_button.click(
                fn=run_current_evaluation_question,
                inputs=[
                    current_question,
                    current_task_id,
                    evaluation_questions,
                    evaluation_answers,
                ],
                outputs=[
                    current_answer,
                    evaluation_answers,
                    run_status,
                    evaluation_progress,
                    evaluation_readiness,
                    results_table,
                ],
            )
            save_answer_button.click(
                fn=save_reviewed_answer,
                inputs=[
                    current_task_id,
                    current_answer,
                    evaluation_questions,
                    evaluation_answers,
                ],
                outputs=[
                    evaluation_answers,
                    run_status,
                    evaluation_progress,
                    evaluation_readiness,
                    results_table,
                ],
            )

        with gr.Tab("3. Enviar resultado"):
            gr.Markdown(
                "O botão abaixo envia os valores atuais da tabela da aba "
                "anterior. Confira todas as respostas antes de continuar."
            )
            submit_button = gr.Button(
                "Enviar respostas revisadas ao leaderboard",
                variant="primary",
            )
            submit_status = gr.Textbox(
                label="Resultado do envio", lines=6, interactive=False
            )
            submit_button.click(
                fn=submit_to_leaderboard,
                inputs=[results_table],
                outputs=[submit_status],
            )


if __name__ == "__main__":
    demo.launch(debug=True, share=False)