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
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
"""
Queue Worker Module
Handles tasks in the request queue
"""

import asyncio
import time
from asyncio import Event, Future, Task
from typing import Callable, Optional, cast

from fastapi import HTTPException, Request
from playwright.async_api import Locator
from playwright.async_api import expect as expect_async

from api_utils.context_types import QueueItem
from models import QuotaExceededError

from .client_connection import check_client_connection


async def queue_worker() -> None:
    """Queue worker, processes tasks in the request queue"""
    # Delayed imports to avoid circularity
    from api_utils.server_state import state
    from config import RESPONSE_COMPLETION_TIMEOUT

    logger = state.logger
    request_queue = state.request_queue
    processing_lock = state.processing_lock
    model_switching_lock = state.model_switching_lock
    params_cache_lock = state.params_cache_lock
    from browser_utils.auth_rotation import perform_auth_rotation
    from browser_utils.page_controller import PageController
    from config.global_state import GlobalState

    from .error_utils import (
        client_cancelled,
        client_disconnected,
        server_error,
    )

    # Internal imports for queue worker logic
    from .request_processor import (
        ClientDisconnectedError,
        _process_request_refactored,
        _test_client_connection,
        save_error_snapshot,
    )
    from .utils_ext.stream import clear_stream_queue

    logger.info("--- Queue Worker Started ---")

    # Validate that required globals are initialized
    if request_queue is None:
        logger.critical("FATAL: request_queue is None! Initialization failed.")
        raise RuntimeError("request_queue not initialized")

    if processing_lock is None:
        logger.critical("FATAL: processing_lock is None! Initialization failed.")
        raise RuntimeError("processing_lock not initialized")

    if model_switching_lock is None:
        logger.critical("FATAL: model_switching_lock is None! Initialization failed.")
        raise RuntimeError("model_switching_lock not initialized")

    if params_cache_lock is None:
        logger.critical("FATAL: params_cache_lock is None! Initialization failed.")
        raise RuntimeError("params_cache_lock not initialized")

    logger.debug(
        f"Queue worker initialized with queue={request_queue}, lock={processing_lock}"
    )

    was_last_request_streaming = False
    last_request_completion_time = 0.0
    shutdown_check_interval = 0.1

    while True:
        request_item: Optional[QueueItem] = None
        result_future: Optional[Future] = None
        http_request: Optional[Request] = None
        req_id: str = "UNKNOWN"
        completion_event: Optional[Event] = None
        submit_btn_loc: Optional[Locator] = None
        client_disco_checker: Optional[Callable[[str], bool]] = None
        disconnect_monitor_task: Optional[Task] = None
        client_disconnected_early: bool = False

        try:
            # [SHUTDOWN] Check shutdown signal
            if GlobalState.IS_SHUTTING_DOWN.is_set():
                logger.info("🚨 Queue Worker detected shutdown signal, exiting.")
                break

            # Clean up disconnected requests in queue
            queue_size = request_queue.qsize()
            if queue_size > 0:
                checked_count = 0
                items_to_requeue = []
                processed_ids = set()

                while checked_count < queue_size and checked_count < 10:
                    if GlobalState.IS_SHUTTING_DOWN.is_set():
                        break
                    try:
                        item = request_queue.get_nowait()
                        item_req_id = item.get("req_id", "unknown")
                        if item_req_id in processed_ids:
                            items_to_requeue.append(item)
                            continue
                        processed_ids.add(item_req_id)

                        if not item.get("cancelled", False):
                            item_http_req = item.get("http_request")
                            if item_http_req:
                                try:
                                    if not await check_client_connection(
                                        item_req_id, item_http_req
                                    ):
                                        logger.info(
                                            f"[{item_req_id}] (Worker Queue Check) Client disconnect detected."
                                        )
                                        item["cancelled"] = True
                                        item_fut = item.get("result_future")
                                        if item_fut and not item_fut.done():
                                            item_fut.set_exception(
                                                client_disconnected(
                                                    item_req_id,
                                                    "Client disconnected while queued.",
                                                )
                                            )
                                except Exception as e:
                                    logger.error(
                                        f"[{item_req_id}] (Worker Queue Check) Error: {e}"
                                    )

                        items_to_requeue.append(item)
                        checked_count += 1
                    except asyncio.QueueEmpty:
                        break

                for item in items_to_requeue:
                    await request_queue.put(item)

            # [AUTH-ROTATION] Handle quota or rotation needs
            if GlobalState.IS_QUOTA_EXCEEDED or GlobalState.NEEDS_ROTATION:
                reason = (
                    "Quota Exceeded"
                    if GlobalState.IS_QUOTA_EXCEEDED
                    else "Graceful Rotation Pending"
                )
                logger.info(f"⏸️ Pausing worker for Auth Rotation ({reason})...")
                GlobalState.start_recovery()
                try:
                    current_model_id = state.current_ai_studio_model_id
                    rotation_success = await perform_auth_rotation(
                        target_model_id=current_model_id or ""
                    )
                    if rotation_success:
                        GlobalState.NEEDS_ROTATION = False
                        logger.info("✅ Auth rotation completed successfully.")
                    else:
                        logger.error("❌ Auth rotation failed.")
                        await asyncio.sleep(1)
                finally:
                    GlobalState.finish_recovery()
                if not rotation_success:
                    continue

            if GlobalState.IS_SHUTTING_DOWN.is_set():
                break

            # Get next request
            try:
                current_timeout = (
                    shutdown_check_interval
                    if GlobalState.IS_SHUTTING_DOWN.is_set()
                    else 5.0
                )
                request_item = await asyncio.wait_for(
                    request_queue.get(), timeout=current_timeout
                )
            except asyncio.TimeoutError:
                continue

            if request_item is None:
                continue

            req_id = request_item["req_id"]
            request_data = request_item["request_data"]
            http_request = request_item["http_request"]
            result_future = request_item["result_future"]

            GlobalState.CURRENT_STREAM_REQ_ID = req_id
            logger.info(f"[{req_id}] (Worker) Processing request dequeued.")

            if GlobalState.IS_QUOTA_EXCEEDED:
                logger.warning(f"[{req_id}] (Worker) ⛔ Quota exceeded, re-queueing.")
                await request_queue.put(request_item)
                request_queue.task_done()
                continue

            if request_item.get("cancelled", False):
                if result_future and not result_future.done():
                    result_future.set_exception(
                        client_cancelled(req_id, "Request cancelled by user")
                    )
                request_queue.task_done()
                continue

            is_streaming_request = request_data.stream

            # Initial connection check
            if not await _test_client_connection(req_id, http_request):
                if result_future and not result_future.done():
                    result_future.set_exception(
                        HTTPException(status_code=499, detail="Client disconnected")
                    )
                request_queue.task_done()
                continue

            # Streaming delay
            current_time = time.time()
            if (
                was_last_request_streaming
                and is_streaming_request
                and (current_time - last_request_completion_time < 1.0)
            ):
                await asyncio.sleep(
                    max(0.5, 1.0 - (current_time - last_request_completion_time))
                )

            # Wait for lock
            async with processing_lock:
                logger.info(f"[{req_id}] (Worker) Lock acquired.")

                if not await _test_client_connection(req_id, http_request):
                    if result_future and not result_future.done():
                        result_future.set_exception(
                            HTTPException(status_code=499, detail="Client disconnected")
                        )
                elif result_future and result_future.done():
                    logger.info(f"[{req_id}] (Worker) Future already done.")
                else:
                    try:
                        returned_value = await _process_request_refactored(
                            req_id, request_data, http_request, result_future
                        )

                        if (
                            isinstance(returned_value, tuple)
                            and len(returned_value) == 3
                        ):
                            completion_event, submit_btn_loc, client_disco_checker = (
                                returned_value
                            )

                        if completion_event:
                            if isinstance(completion_event, dict):
                                if (
                                    completion_event.get("done")
                                    and is_streaming_request
                                ):
                                    if state.STREAM_QUEUE:
                                        await state.STREAM_QUEUE.put(completion_event)
                                if result_future and not result_future.done():
                                    result_future.set_result(completion_event)
                                client_disconnected_early = False
                            elif hasattr(completion_event, "wait"):
                                client_disconnected_early = False
                                comp_ev = cast(Event, completion_event)

                                async def enhanced_disconnect_monitor_fn():
                                    nonlocal client_disconnected_early
                                    disco_count = 0
                                    while not comp_ev.is_set():
                                        if GlobalState.IS_SHUTTING_DOWN.is_set():
                                            comp_ev.set()
                                            break
                                        if (
                                            GlobalState.IS_QUOTA_EXCEEDED
                                            and not GlobalState.IS_RECOVERING
                                        ):
                                            # Abort if quota exceeded and not recovering
                                            client_disconnected_early = True
                                            comp_ev.set()
                                            break

                                        if not await _test_client_connection(
                                            req_id, http_request
                                        ):
                                            disco_count += 1
                                            if disco_count >= 3:
                                                client_disconnected_early = True
                                                comp_ev.set()
                                                break
                                        else:
                                            disco_count = 0
                                        await asyncio.sleep(0.2)

                                disconnect_monitor_task = asyncio.create_task(
                                    enhanced_disconnect_monitor_fn()
                                )
                                await asyncio.wait_for(
                                    comp_ev.wait(),
                                    timeout=RESPONSE_COMPLETION_TIMEOUT / 1000 + 60,
                                )
                        else:
                            # Non-streaming
                            client_disconnected_early = False
                            res_fut = cast(Future, result_future)

                            async def non_streaming_monitor_fn():
                                nonlocal client_disconnected_early
                                while not res_fut.done():
                                    if GlobalState.IS_SHUTTING_DOWN.is_set():
                                        res_fut.cancel()
                                        break
                                    if not await _test_client_connection(
                                        req_id, http_request
                                    ):
                                        client_disconnected_early = True
                                        res_fut.set_exception(
                                            HTTPException(
                                                status_code=499,
                                                detail="Client disconnected",
                                            )
                                        )
                                        break
                                    await asyncio.sleep(0.3)

                            disconnect_monitor_task = asyncio.create_task(
                                non_streaming_monitor_fn()
                            )
                            await asyncio.wait_for(
                                asyncio.shield(res_fut),
                                timeout=RESPONSE_COMPLETION_TIMEOUT / 1000 + 60,
                            )

                        # Post-processing button handling
                        if client_disconnected_early:
                            if submit_btn_loc:
                                try:
                                    if await submit_btn_loc.is_enabled(timeout=2000):
                                        await submit_btn_loc.click(
                                            timeout=5000, force=True
                                        )
                                except Exception:
                                    pass
                        elif (
                            submit_btn_loc and client_disco_checker and completion_event
                        ):
                            try:
                                client_disco_checker("Post-stream check")
                                await asyncio.sleep(0.5)
                                client_disco_checker("Post-sleep check")
                                if await submit_btn_loc.is_enabled(timeout=2000):
                                    await submit_btn_loc.click(timeout=5000, force=True)
                                await expect_async(submit_btn_loc).to_be_disabled(
                                    timeout=10000
                                )
                            except ClientDisconnectedError:
                                pass
                            except Exception:
                                await save_error_snapshot(f"button_timeout_{req_id}")

                    except QuotaExceededError:
                        raise
                    except Exception as e:
                        logger.error(f"[{req_id}] (Worker) Error: {e}")
                        if result_future and not result_future.done():
                            result_future.set_exception(
                                server_error(req_id, f"Error: {e}")
                            )
                    finally:
                        if (
                            disconnect_monitor_task
                            and not disconnect_monitor_task.done()
                        ):
                            disconnect_monitor_task.cancel()
                            try:
                                await disconnect_monitor_task
                            except asyncio.CancelledError:
                                pass

            # [ROTATION] Post-request rotation check
            just_rotated = False
            if GlobalState.NEEDS_ROTATION:
                current_model_id_rot = state.current_ai_studio_model_id
                if await perform_auth_rotation(
                    target_model_id=current_model_id_rot or ""
                ):
                    GlobalState.NEEDS_ROTATION = False
                    just_rotated = True

            # [CLEANUP]
            try:
                await clear_stream_queue()

                # [COOKIE-REFRESH] Save cookies after successful requests
                if not client_disconnected_early and not GlobalState.IS_QUOTA_EXCEEDED:
                    try:
                        from browser_utils.cookie_refresh import (
                            maybe_refresh_on_request,
                        )

                        await maybe_refresh_on_request()
                    except Exception as cookie_err:
                        logger.debug(
                            f"[{req_id}] Cookie refresh error (non-critical): {cookie_err}"
                        )

                if (
                    not GlobalState.IS_QUOTA_EXCEEDED
                    and not just_rotated
                    and not GlobalState.IS_SHUTTING_DOWN.is_set()
                ):
                    if submit_btn_loc and client_disco_checker:
                        s_page = state.page_instance
                        s_ready = state.is_page_ready
                        s_browser = state.browser_instance

                        if (
                            s_page
                            and s_ready
                            and s_browser
                            and s_browser.is_connected()
                        ):
                            try:
                                controller = PageController(s_page, logger, req_id)
                                await controller.clear_chat_history(lambda stage: False)
                            except Exception:
                                try:
                                    await s_page.reload()
                                except Exception:
                                    pass
            except Exception as e:
                logger.error(f"[{req_id}] Cleanup error: {e}")

            was_last_request_streaming = is_streaming_request
            last_request_completion_time = time.time()

        except asyncio.CancelledError:
            if result_future and not result_future.done():
                result_future.cancel()
            break
        except QuotaExceededError:
            try:
                if await _test_client_connection(req_id, http_request):
                    request_queue.put_nowait(request_item)
                elif result_future and not result_future.done():
                    result_future.set_exception(
                        HTTPException(
                            status_code=499, detail="Disconnected during quota error"
                        )
                    )
            except Exception:
                pass
        except Exception as e:
            logger.error(f"[{req_id}] Unexpected error: {e}", exc_info=True)
            if result_future and not result_future.done():
                result_future.set_exception(server_error(req_id, f"Error: {e}"))
        finally:
            if request_item:
                request_queue.task_done()

    logger.info("--- Queue Worker Stopped ---")