File size: 49,498 Bytes
22087d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Agent runs API endpoints.

Thin API layer - delegates to core modules:
- File handling: core/files/upload_handler.py
- Execution: core/run/agent_runner.py
"""

import asyncio
import json
import os
import time
import traceback
import uuid
from datetime import datetime, timezone
from typing import Optional, List, Dict, Any, Tuple

from fastapi import APIRouter, HTTPException, Depends, Request, File, UploadFile, Form
from fastapi.responses import StreamingResponse

from core.utils.auth_utils import (
    verify_and_get_user_id_from_jwt, 
    get_user_id_from_stream_auth, 
    verify_and_authorize_thread_access
)
from core.utils.logger import logger, structlog
from core.billing.credits.integration import billing_integration
from core.utils.config import config, EnvMode
from core.services import redis
from core.ai_models import model_manager
from core.api_models import UnifiedAgentStartResponse
from core.services.supabase import DBConnection

# Import from new modules
from core.agents.runner import execute_agent_run

db = DBConnection()
router = APIRouter(tags=["agent-runs"])

# Store cancellation events for stop mechanism (in-memory, per instance)
_cancellation_events: Dict[str, asyncio.Event] = {}


# ============================================================================
# Helper Functions
# ============================================================================

async def _get_agent_run_with_access_check(agent_run_id: str, user_id: str, require_write_access: bool = False):
    """Get agent run with access check."""
    from core.agents import repo as agents_repo
    
    agent_run_data = await agents_repo.get_agent_run_with_thread(agent_run_id)
    if not agent_run_data:
        raise HTTPException(status_code=404, detail="Worker run not found")

    thread_id = agent_run_data['thread_id']
    account_id = agent_run_data['thread_account_id']
    metadata = agent_run_data.get('metadata', {}) or {}
    actual_user_id = metadata.get('actual_user_id')
    
    if actual_user_id == user_id or account_id == user_id:
        return agent_run_data
    
    client = await db.client
    await verify_and_authorize_thread_access(client, thread_id, user_id, require_write_access=require_write_access)
    return agent_run_data


async def _check_billing_and_limits(
    client, 
    account_id: str, 
    model_name: Optional[str], 
    check_project_limit: bool = False, 
    check_thread_limit: bool = False
):
    import time
    from core.billing.subscriptions import subscription_service
    from core.utils.limits_repo import get_all_limits_counts
    from core.cache.runtime_cache import get_cached_tier_info
    
    t_start = time.time()
    
    # Skip all checks in local mode
    if config.ENV_MODE == EnvMode.LOCAL:
        return
    
    # Step 1: Get tier info ONCE (with caching)
    # Note: subscription_service.get_user_subscription_tier already handles caching internally
    tier_info = await get_cached_tier_info(account_id)
    if not tier_info:
        tier_info = await subscription_service.get_user_subscription_tier(account_id, skip_cache=False)
        # No need to call set_cached_tier_info here - it's already done inside get_user_subscription_tier
    
    tier_name = tier_info.get('name', 'free')
    
    # Step 2: Run billing check and limits check in parallel
    async def check_billing():
        if model_name == "mock-ai":
            return (True, None, {})
        
        # Check model access
        from core.billing.shared.config import is_model_allowed
        if not is_model_allowed(tier_name, model_name):
            available_models = tier_info.get('models', [])
            return False, f"Your current subscription plan does not include access to {model_name}. Please upgrade your subscription.", {
                "allowed_models": available_models,
                "tier_info": tier_info,
                "tier_name": tier_name,
                "error_type": "model_access_denied",
                "error_code": "MODEL_ACCESS_DENIED"
            }
        
        # Check credits
        can_run, message, reservation_id = await billing_integration.check_and_reserve_credits(account_id)
        if not can_run:
            return False, f"Billing check failed: {message}", {
                "tier_info": tier_info,
                "error_type": "insufficient_credits"
            }
        
        return True, "Access granted", {"tier_info": tier_info, "reservation_id": reservation_id}
    
    async def check_all_limits():
        """Get all limits in a single DB query."""
        # Use the optimized single-query function
        counts = await get_all_limits_counts(account_id)
        
        # Get tier limits
        concurrent_limit = tier_info.get('concurrent_runs', 1)
        project_limit = tier_info.get('project_limit', 3)
        thread_limit = tier_info.get('thread_limit', 10)
        
        return {
            'agent_runs': {
                'can_start': counts['running_runs_count'] < concurrent_limit,
                'running_count': counts['running_runs_count'],
                'limit': concurrent_limit,
            },
            'projects': {
                'can_create': counts['project_count'] < project_limit,
                'current_count': counts['project_count'],
                'limit': project_limit,
            },
            'threads': {
                'can_create': counts['thread_count'] < thread_limit,
                'current_count': counts['thread_count'],
                'limit': thread_limit,
            }
        }
    
    # Run both in parallel
    billing_result, limits_result = await asyncio.gather(
        check_billing(), check_all_limits()
    )
    
    logger.debug(f"⚡ Billing + limits check completed in {(time.time() - t_start) * 1000:.1f}ms")
    
    # Process billing result
    can_proceed, error_message, context = billing_result
    if not can_proceed:
        error_type = context.get("error_type")
        if error_type == "model_access_denied":
            raise HTTPException(status_code=402, detail={
                "message": error_message, 
                "tier_name": context.get("tier_name"),
                "error_code": "MODEL_ACCESS_DENIED"
            })
        elif error_type == "insufficient_credits":
            raise HTTPException(status_code=402, detail={
                "message": error_message,
                "error_code": "INSUFFICIENT_CREDITS"
            })
        else:
            raise HTTPException(status_code=500, detail={"message": error_message})
    
    # Check agent run limit
    agent_run_result = limits_result['agent_runs']
    if not agent_run_result.get('can_start', True):
        # Need to fetch running thread IDs for the error response
        from core.utils.limits_repo import count_running_agent_runs
        run_details = await count_running_agent_runs(account_id)
        running_ids = [str(tid) for tid in run_details.get('running_thread_ids', [])]
        raise HTTPException(status_code=402, detail={
            "message": f"Maximum of {agent_run_result['limit']} concurrent runs. You have {agent_run_result['running_count']} running.",
            "running_thread_ids": running_ids,
            "running_count": agent_run_result['running_count'],
            "limit": agent_run_result['limit'],
            "error_code": "AGENT_RUN_LIMIT_EXCEEDED"
        })

    # Check project limit
    project_result = limits_result['projects']
    if check_project_limit and not project_result.get('can_create', True):
        raise HTTPException(status_code=402, detail={
            "message": f"Maximum of {project_result['limit']} projects allowed.",
            "current_count": project_result['current_count'],
            "limit": project_result['limit'],
            "error_code": "PROJECT_LIMIT_EXCEEDED"
        })
    
    # Check thread limit
    thread_result = limits_result['threads']
    if check_thread_limit and not thread_result.get('can_create', True):
        raise HTTPException(status_code=402, detail={
            "message": f"Maximum of {thread_result['limit']} threads allowed.",
            "current_count": thread_result['current_count'],
            "limit": thread_result['limit'],
            "error_code": "THREAD_LIMIT_EXCEEDED"
        })


async def _check_concurrent_runs_limit(account_id: str):
    from core.cache.runtime_cache import get_cached_running_runs, get_cached_tier_info
    from core.billing.subscriptions import subscription_service
    
    tier_info = await get_cached_tier_info(account_id)
    if not tier_info:
        concurrent_limit = 1
    else:
        concurrent_limit = tier_info.get('concurrent_runs', 1)
    
    cached_runs = await get_cached_running_runs(account_id)
    if cached_runs is not None:
        running_count = len(cached_runs) if isinstance(cached_runs, list) else cached_runs
        if running_count >= concurrent_limit:
            from core.utils.limits_repo import count_running_agent_runs
            run_details = await count_running_agent_runs(account_id)
            running_ids = [str(tid) for tid in run_details.get('running_thread_ids', [])]
            raise HTTPException(status_code=402, detail={
                "message": f"Maximum of {concurrent_limit} concurrent runs. You have {running_count} running.",
                "running_thread_ids": running_ids,
                "running_count": running_count,
                "limit": concurrent_limit,
                "error_code": "AGENT_RUN_LIMIT_EXCEEDED"
            })
        return
    
    from core.utils.limits_repo import count_running_agent_runs
    run_details = await count_running_agent_runs(account_id)
    running_count = run_details.get('count', 0)
    
    if running_count >= concurrent_limit:
        running_ids = [str(tid) for tid in run_details.get('running_thread_ids', [])]
        raise HTTPException(status_code=402, detail={
            "message": f"Maximum of {concurrent_limit} concurrent runs. You have {running_count} running.",
            "running_thread_ids": running_ids,
            "running_count": running_count,
            "limit": concurrent_limit,
            "error_code": "AGENT_RUN_LIMIT_EXCEEDED"
        })


async def _get_effective_model(model_name: Optional[str], agent_config: Optional[dict], client, account_id: str) -> str:
    if model_name:
        return model_name
    elif agent_config and agent_config.get('model'):
        return agent_config['model']
    else:
        return await model_manager.get_default_model_for_user(client, account_id)


async def _create_agent_run_record(
    thread_id: str, 
    agent_config: Optional[dict], 
    effective_model: str, 
    actual_user_id: str,
    extra_metadata: Optional[Dict[str, Any]] = None
) -> str:
    """Create agent run record in database."""
    from core.agents import repo as agents_repo
    from core.utils.instance import get_instance_id
    
    # Include instance_id for multi-instance awareness
    run_metadata = {
        "model_name": effective_model, 
        "actual_user_id": actual_user_id,
        "instance_id": get_instance_id()
    }
    if extra_metadata:
        run_metadata.update(extra_metadata)
    
    agent_run = await agents_repo.create_agent_run(
        thread_id=thread_id,
        agent_id=agent_config.get('agent_id') if agent_config else None,
        agent_version_id=agent_config.get('current_version_id') if agent_config else None,
        metadata=run_metadata
    )

    agent_run_id = agent_run['id']
    structlog.contextvars.bind_contextvars(agent_run_id=agent_run_id)

    # Invalidate caches
    try:
        from core.cache.runtime_cache import invalidate_running_runs_cache
        await invalidate_running_runs_cache(actual_user_id)
    except Exception:
        pass
    
    try:
        from core.billing.shared.cache_utils import invalidate_account_state_cache
        await invalidate_account_state_cache(actual_user_id)
    except Exception:
        pass

    return agent_run_id


async def start_agent_run(
    account_id: str,
    prompt: str,
    agent_id: Optional[str] = None,
    model_name: Optional[str] = None,
    thread_id: Optional[str] = None,
    project_id: Optional[str] = None,
    metadata: Optional[Dict[str, Any]] = None,
    skip_limits_check: bool = False,
    memory_enabled: Optional[bool] = None,
    is_optimistic: bool = False,
    emit_timing: bool = False,
    mode: Optional[str] = None,
    files_data: Optional[List[Tuple[str, bytes, str, Optional[str]]]] = None,
) -> Dict[str, Any]:
    from core.agents.config import load_agent_config_fast
    from core.agents.pipeline.slot_manager import (
        reserve_slot,
        check_thread_limit,
        check_project_limit,
    )
    from core.agents.pipeline.time_estimator import time_estimator
    from core.agents.pipeline.ux_streaming import stream_ack, stream_estimate
    
    total_start = time.time()
    is_new_thread = thread_id is None or is_optimistic
    
    logger.info(f"🚀 start_agent_run: is_optimistic={is_optimistic}, is_new_thread={is_new_thread}")
    
    agent_config = await load_agent_config_fast(agent_id, account_id, user_id=account_id)
    
    if model_name:
        effective_model = model_name
    elif agent_config and agent_config.get('model'):
        effective_model = agent_config['model']
    else:
        effective_model = "anthropic/claude-sonnet-4-20250514"
    
    if not is_new_thread and not project_id:
        from core.threads import repo as threads_repo
        project_id = await threads_repo.get_thread_project_id(thread_id)
    
    if is_new_thread and not skip_limits_check:
        thread_check, project_check = await asyncio.gather(
            check_thread_limit(account_id, skip=skip_limits_check),
            check_project_limit(account_id, skip=skip_limits_check),
        )
        
        if not thread_check.allowed:
            logger.warning(f"⚠️ Thread limit exceeded for {account_id}: {thread_check.current_count}/{thread_check.limit}")
            raise HTTPException(
                status_code=402,
                detail={
                    "message": thread_check.message,
                    "current_count": thread_check.current_count,
                    "limit": thread_check.limit,
                    "error_code": thread_check.error_code or "THREAD_LIMIT_EXCEEDED"
                }
            )
        
        if not project_check.allowed:
            logger.warning(f"⚠️ Project limit exceeded for {account_id}: {project_check.current_count}/{project_check.limit}")
            raise HTTPException(
                status_code=402,
                detail={
                    "message": project_check.message,
                    "current_count": project_check.current_count,
                    "limit": project_check.limit,
                    "error_code": project_check.error_code or "PROJECT_LIMIT_EXCEEDED"
                }
            )
    
    if is_new_thread:
        if not project_id:
            project_id = str(uuid.uuid4())
        if not thread_id:
            thread_id = str(uuid.uuid4())
    
    agent_run_id = str(uuid.uuid4())
    
    # Sandbox handling - always start sandbox creation for new threads
    sandbox_id = None
    from core.threads import repo as threads_repo
    from core.sandbox.resolver import resolve_sandbox
    client = await db.client
    
    has_files = files_data and len(files_data) > 0
    
    if has_files:
        # WITH FILES: Create sandbox (blocking) and upload files immediately
        # Ensure project exists first (needed for sandbox linking)
        if is_new_thread:
            placeholder_name = f"{prompt[:30]}..." if len(prompt) > 30 else prompt if prompt else "Untitled"
            try:
                await threads_repo.create_project(project_id, account_id, placeholder_name)
            except Exception as e:
                logger.debug(f"Project creation skipped (may already exist): {e}")
        else:
            try:
                existing_project = await threads_repo.get_project(project_id)
                if not existing_project:
                    placeholder_name = f"{prompt[:30]}..." if len(prompt) > 30 else prompt if prompt else "Untitled"
                    await threads_repo.create_project(project_id, account_id, placeholder_name)
            except Exception as e:
                logger.debug(f"Project check/creation skipped: {e}")
        
        # Resolve sandbox (creates if needed, starts it) - BLOCKING
        sandbox_info = await resolve_sandbox(project_id, account_id, client, require_started=True)
        sandbox_id = sandbox_info.sandbox_id if sandbox_info else None
        
        if sandbox_id:
            logger.info(f"✅ [AGENT_START] Resolved sandbox {sandbox_id} for file uploads (project {project_id})")
            # Upload files immediately
            from core.files.upload_handler import upload_files_to_sandbox
            await upload_files_to_sandbox(project_id, thread_id, files_data, account_id)
            logger.info(f"✅ [AGENT_START] Uploaded {len(files_data)} files to sandbox {sandbox_id}")
        else:
            logger.warning(f"⚠️ [AGENT_START] Failed to resolve sandbox for file uploads (project {project_id})")
    
    elif is_new_thread:
        # NO FILES but NEW THREAD: Start sandbox creation in background (non-blocking)
        # This ensures sandbox is ready by the time user checks Files tab
        async def create_sandbox_background(proj_id: str, acc_id: str, db_client):
            try:
                # First ensure project exists
                placeholder_name = f"{prompt[:30]}..." if len(prompt) > 30 else prompt if prompt else "Untitled"
                try:
                    await threads_repo.create_project(proj_id, acc_id, placeholder_name)
                except Exception:
                    pass  # May already exist
                
                # Then create sandbox
                sandbox_info = await resolve_sandbox(proj_id, acc_id, db_client, require_started=True)
                if sandbox_info:
                    logger.info(f"✅ [BACKGROUND] Created sandbox {sandbox_info.sandbox_id} for project {proj_id}")
                else:
                    logger.warning(f"⚠️ [BACKGROUND] Failed to create sandbox for project {proj_id}")
            except Exception as e:
                logger.error(f"❌ [BACKGROUND] Error creating sandbox for project {proj_id}: {e}")
        
        # Fire and forget - don't wait for sandbox creation
        asyncio.create_task(create_sandbox_background(project_id, account_id, client))
        logger.info(f"🚀 [AGENT_START] Started background sandbox creation for new project {project_id}")
    
    slot_reservation = await reserve_slot(
        account_id=account_id,
        agent_run_id=agent_run_id,
        skip=skip_limits_check
    )
    
    if not slot_reservation.acquired:
        logger.warning(f"⚠️ Slot rejected for {agent_run_id}: {slot_reservation.message}")
        raise HTTPException(
            status_code=402,
            detail={
                "message": slot_reservation.message,
                "running_count": slot_reservation.slot_count,
                "limit": slot_reservation.limit,
                "error_code": slot_reservation.error_code or "AGENT_RUN_LIMIT_EXCEEDED"
            }
        )
    
    cancellation_event = asyncio.Event()
    _cancellation_events[agent_run_id] = cancellation_event

    try:
        stream_key = f"agent_run:{agent_run_id}:stream"

        asyncio.create_task(stream_ack(stream_key, agent_run_id))

        has_mcp = bool(agent_config and agent_config.get('mcp_servers'))
        estimate = time_estimator.estimate(
            model_name=effective_model,
            has_mcp=has_mcp,
            is_continuation=False
        )
        asyncio.create_task(stream_estimate(
            stream_key,
            estimate.estimated_seconds,
            estimate.confidence,
            estimate.breakdown.to_dict()
        ))

        if is_new_thread:
            from core.cache.runtime_cache import set_pending_thread, set_agent_run_stream_data
            await set_pending_thread(
                thread_id=thread_id,
                project_id=project_id,
                account_id=account_id,
                agent_run_id=agent_run_id,
                prompt=prompt
            )
            await set_agent_run_stream_data(
                agent_run_id=agent_run_id,
                thread_id=thread_id,
                account_id=account_id,
                status="running",
                metadata=metadata
            )
        else:
            from core.cache.runtime_cache import set_agent_run_stream_data
            await set_agent_run_stream_data(
                agent_run_id=agent_run_id,
                thread_id=thread_id,
                account_id=account_id,
                status="running",
                metadata=metadata
            )
    except Exception:
        # Clean up cancellation event if setup between event creation
        # and background task launch fails
        _cancellation_events.pop(agent_run_id, None)
        raise

    setup_time_ms = round((time.time() - total_start) * 1000, 1)
    logger.info(f"⚡ [FAST RESPONSE] Returning in {setup_time_ms}ms (thread={thread_id}, run={agent_run_id})")

    logger.info(f"[START_AGENT] Creating background task for agent_run_id={agent_run_id}, thread_id={thread_id}")
    asyncio.create_task(_background_setup_and_execute(
        account_id=account_id,
        prompt=prompt,
        thread_id=thread_id,
        project_id=project_id,
        agent_run_id=agent_run_id,
        agent_config=agent_config,
        effective_model=effective_model,
        metadata=metadata,
        memory_enabled=memory_enabled,
        is_new_thread=is_new_thread,
        mode=mode,
        cancellation_event=cancellation_event,
        skip_limits_check=skip_limits_check,
    ))
    
    return {
        "thread_id": thread_id,
        "agent_run_id": agent_run_id,
        "project_id": project_id,
        "sandbox_id": sandbox_id,  # NEW
        "status": "running",
        "timing_breakdown": {"setup_ms": setup_time_ms} if emit_timing else None,
    }


async def _background_setup_and_execute(
    account_id: str,
    prompt: str,
    thread_id: str,
    project_id: str,
    agent_run_id: str,
    agent_config: dict,
    effective_model: str,
    metadata: Optional[Dict[str, Any]],
    memory_enabled: Optional[bool],
    is_new_thread: bool,
    mode: Optional[str],
    cancellation_event: asyncio.Event,
    skip_limits_check: bool = False,
):
    from core.utils.lifecycle_tracker import log_run_start, log_run_cleanup
    from core.agents.runner.setup_manager import (
        prepopulate_caches_for_new_thread,
        append_user_message_to_cache,
        write_user_message_for_existing_thread,
        prewarm_user_context,
        create_new_thread_records,
        create_agent_run_record,
        create_image_messages,
        notify_setup_error,
    )
    
    structlog.contextvars.clear_contextvars()
    structlog.contextvars.bind_contextvars(
        agent_run_id=agent_run_id, 
        thread_id=thread_id, 
        project_id=project_id,
        account_id=account_id
    )
    
    logger.info(f"[BG_TASK_START] Background task starting for agent_run_id={agent_run_id}")
    
    try:
        final_message_content = prompt
        image_contexts_to_inject = []
        
        # Note: Project and sandbox are now created upfront in start_agent_run()
        # Files are uploaded directly to sandbox by frontend after receiving sandbox_id
        
        if is_new_thread:
            await prepopulate_caches_for_new_thread(
                thread_id=thread_id,
                project_id=project_id,
                message_content=final_message_content,
                image_contexts=image_contexts_to_inject,
                mode=mode,
            )
            cache_updated = False
        else:
            cache_updated = await append_user_message_to_cache(thread_id, final_message_content)
            if not cache_updated:
                logger.debug(f"⚠️ Cache miss for thread {thread_id}, writing message to DB first")
                await write_user_message_for_existing_thread(thread_id, final_message_content)
        
        logger.debug(f"⚡ [BG] Caches ready, starting agent + DB writes in parallel")
        
        asyncio.create_task(prewarm_user_context(account_id))
        
        from core.agents.runner.setup_manager import prewarm_credit_balance
        credit_prewarm_task = asyncio.create_task(prewarm_credit_balance(account_id))
        
        async def do_db_writes():
            db_start = time.time()
            try:
                if is_new_thread:
                    await create_new_thread_records(
                        project_id=project_id,
                        thread_id=thread_id,
                        account_id=account_id,
                        prompt=prompt,
                        agent_run_id=agent_run_id,
                        message_content=final_message_content,
                        agent_config=agent_config,
                        metadata=metadata,
                        memory_enabled=memory_enabled,
                    )
                    from core.agents.pipeline.slot_manager import (
                        increment_thread_count,
                        increment_project_count,
                    )
                    await asyncio.gather(
                        increment_thread_count(account_id),
                        increment_project_count(account_id),
                    )
                else:
                    if cache_updated:
                        await write_user_message_for_existing_thread(thread_id, final_message_content)
                    await create_agent_run_record(
                        agent_run_id=agent_run_id,
                        thread_id=thread_id,
                        agent_config=agent_config,
                        effective_model=effective_model,
                        account_id=account_id,
                        extra_metadata=metadata,
                    )
                
                await create_image_messages(thread_id, image_contexts_to_inject)
                logger.debug(f"⏱️ [BG] DB writes completed: {(time.time() - db_start)*1000:.1f}ms")
            except Exception as e:
                logger.error(f"❌ [BG] DB write failed: {e}")
        
        log_run_start(agent_run_id, thread_id)
        
        db_task = asyncio.create_task(do_db_writes())
        logger.info(f"✅ [BG] Starting agent execution")
        
        cleanup_reason = None
        final_status = "unknown"
        cleanup_errors = []
        run_timeout_seconds = int(os.getenv("AGENT_RUN_TIMEOUT_SECONDS", "3000"))  # 65min, > MAX_DURATION so cleanup always runs
        
        try:
            await asyncio.wait_for(
                execute_agent_run(
                    agent_run_id=agent_run_id,
                    thread_id=thread_id,
                    project_id=project_id,
                    model_name=effective_model,
                    agent_config=agent_config,
                    account_id=account_id,
                    cancellation_event=cancellation_event,
                    is_new_thread=is_new_thread,
                    user_message=final_message_content
                ),
                timeout=run_timeout_seconds,
            )
            final_status = "completed"
        except asyncio.TimeoutError:
            cancellation_event.set()
            final_status = "failed"
            cleanup_reason = f"run_timeout ({run_timeout_seconds}s)"
            logger.error(f"[LIFECYCLE] RUN TIMEOUT agent_run={agent_run_id} after {run_timeout_seconds}s")
            try:
                from core.agents.runner import update_agent_run_status
                await update_agent_run_status(agent_run_id, "failed", error=cleanup_reason, account_id=account_id)
            except Exception as e:
                logger.warning(f"[LIFECYCLE] Failed to update status on timeout: {e}")
        except asyncio.CancelledError:
            final_status = "cancelled"
            cleanup_reason = "Task cancelled"
        except Exception as e:
            final_status = "failed"
            cleanup_reason = f"{type(e).__name__}: {str(e)[:100]}"
            logger.error(f"[LIFECYCLE] EXCEPTION agent_run={agent_run_id} error={cleanup_reason}")
        finally:
            try:
                from core.agents.pipeline.slot_manager import release_slot
                await release_slot(account_id, agent_run_id)
            except Exception as slot_err:
                logger.error(f"[SLOT] Failed to release slot for {agent_run_id}: {slot_err}")
                cleanup_errors.append(f"slot_release: {slot_err}")
            
            try:
                await asyncio.wait_for(db_task, timeout=30.0)
            except asyncio.TimeoutError:
                logger.warning(f"⚠️ [BG] DB writes timed out after 30s")
            except Exception as e:
                logger.warning(f"⚠️ [BG] DB writes failed: {e}")
            
            was_in_events = _cancellation_events.pop(agent_run_id, None) is not None
            if not was_in_events:
                cleanup_errors.append("not_in_cancellation_events")
            
            log_run_cleanup(
                agent_run_id, 
                success=(cleanup_reason is None),
                reason=cleanup_reason,
                final_status=final_status,
                cleanup_errors=cleanup_errors if cleanup_errors else None
            )
    
    except Exception as e:
        logger.error(f"❌ [BG] Setup failed for agent_run={agent_run_id}: {e}")
        # Release slot on setup failure too
        try:
            from core.agents.pipeline.slot_manager import release_slot
            await release_slot(account_id, agent_run_id)
        except Exception:
            pass
        _cancellation_events.pop(agent_run_id, None)
        await notify_setup_error(agent_run_id, e)


@router.post("/agent/start", response_model=UnifiedAgentStartResponse, summary="Start Agent", operation_id="unified_agent_start")
async def unified_agent_start(
    request: Request,
    thread_id: Optional[str] = Form(None),
    project_id: Optional[str] = Form(None),
    prompt: Optional[str] = Form(None),
    model_name: Optional[str] = Form(None),
    agent_id: Optional[str] = Form(None),
    optimistic: Optional[str] = Form(None),
    memory_enabled: Optional[str] = Form(None),
    mode: Optional[str] = Form(None),
    files: List[UploadFile] = File(default=[]),
    user_id: str = Depends(verify_and_get_user_id_from_jwt)
):
    client = await db.client
    account_id = user_id
    is_optimistic = optimistic and optimistic.lower() == 'true'
    
    if is_optimistic:
        if not thread_id or not project_id:
            raise HTTPException(status_code=400, detail="thread_id and project_id required for optimistic mode")
        if not prompt or not prompt.strip():
            raise HTTPException(status_code=400, detail="prompt required for optimistic mode")
        try:
            uuid.UUID(thread_id)
            uuid.UUID(project_id)
        except ValueError:
            raise HTTPException(status_code=400, detail="Invalid UUID format")
    
    if not is_optimistic and not thread_id and (not prompt or not prompt.strip()):
        raise HTTPException(status_code=400, detail="prompt required when creating new thread")
    
    if model_name is None:
        model_name = await model_manager.get_default_model_for_user(client, account_id)
    elif model_name != "mock-ai":
        model_name = model_manager.resolve_model_id(model_name)

    if model_name in ("kortix/basic", "kortix-basic") and config.ENV_MODE != EnvMode.LOCAL:
        from core.billing.subscriptions import subscription_service
        from core.cache.runtime_cache import get_cached_tier_info
        tier_info = await get_cached_tier_info(account_id) or await subscription_service.get_user_subscription_tier(account_id)
        if tier_info.get('name') in ('free', 'none'):
            logger.info(f"⚡ [MODEL_OVERRIDE] Free tier user {account_id} - using minimax instead of basic")
            model_name = "kortix/minimax"

    memory_enabled_bool = memory_enabled.lower() == 'true' if memory_enabled else None
    
    skip_limits = False
    emit_timing = False
    if request.headers.get("X-Skip-Limits", "").lower() == "true":
        from core.endpoints.user_roles_repo import get_user_admin_role
        role_info = await get_user_admin_role(user_id)
        if role_info.get('role') == 'super_admin':
            skip_limits = True
            emit_timing = request.headers.get("X-Emit-Timing", "").lower() == "true"
    
    try:
        if thread_id and not is_optimistic:
            from core.threads import repo as threads_repo
            thread_data = await threads_repo.get_thread_with_project(thread_id)
            if not thread_data:
                raise HTTPException(status_code=404, detail="Thread not found")
            project_id = thread_data['project_id']
            if thread_data['account_id'] != user_id:
                await verify_and_authorize_thread_access(client, thread_id, user_id, require_write_access=True)
        
        # Parse files if present
        files_data = []
        final_prompt = prompt or ""
        if files:
            from core.files.upload_handler import fast_parse_files
            final_prompt, files_data = await fast_parse_files(files, final_prompt)
            logger.info(f"[AGENT_START] Received {len(files)} files, parsed {len(files_data)} for upload")

        result = await start_agent_run(
            account_id=account_id,
            prompt=final_prompt,
            agent_id=agent_id,
            model_name=model_name,
            thread_id=thread_id,
            project_id=project_id,
            memory_enabled=memory_enabled_bool,
            is_optimistic=is_optimistic,
            skip_limits_check=skip_limits,
            emit_timing=emit_timing,
            mode=mode,
            files_data=files_data if files_data else None,
        )
        
        response = {
            "thread_id": result["thread_id"],
            "agent_run_id": result["agent_run_id"],
            "project_id": result.get("project_id"),
            "sandbox_id": result.get("sandbox_id"),
            "status": result.get("status", "running")
        }
        if emit_timing and result.get("timing_breakdown"):
            response["timing_breakdown"] = result["timing_breakdown"]
        return response
    
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error in agent start: {str(e)}\n{traceback.format_exc()}")
        raise HTTPException(status_code=500, detail=f"Failed to start agent: {str(e)}")


@router.post("/agent-run/{agent_run_id}/stop", summary="Stop Agent Run", operation_id="stop_agent_run")
@router.post("/agent-runs/{agent_run_id}/stop", summary="Stop Agent Run (plural)", include_in_schema=False)
async def stop_agent(agent_run_id: str, user_id: str = Depends(verify_and_get_user_id_from_jwt)):
    """Stop an agent run."""
    from core.utils.run_management import stop_agent_run_with_helpers as stop_agent_run
    
    structlog.contextvars.bind_contextvars(agent_run_id=agent_run_id)
    await _get_agent_run_with_access_check(agent_run_id, user_id, require_write_access=True)
    await stop_agent_run(agent_run_id)
    return {"status": "stopped"}


@router.get("/agent-runs/{agent_run_id}/status", summary="Get Agent Run Status", operation_id="get_agent_run_status")
async def get_agent_run_status(agent_run_id: str, user_id: str = Depends(verify_and_get_user_id_from_jwt)):
    """Get agent run status."""
    agent_run_data = await _get_agent_run_with_access_check(agent_run_id, user_id)
    return {"status": agent_run_data['status'], "error": agent_run_data.get('error')}


@router.get("/agent-runs/active", summary="List Active Agent Runs", operation_id="list_active_agent_runs")
async def get_active_agent_runs(user_id: str = Depends(verify_and_get_user_id_from_jwt)):
    """List all active agent runs for user."""
    from core.agents.repo import get_active_agent_runs as repo_get_active_runs
    
    try:
        active_runs = await repo_get_active_runs(user_id)
        return {"active_runs": active_runs}
    except Exception as e:
        logger.error(f"Error fetching active runs: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Failed to fetch active runs: {str(e)}")


@router.get("/thread/{thread_id}/agent-runs", summary="List Thread Agent Runs", operation_id="list_thread_agent_runs")
async def get_agent_runs(thread_id: str, user_id: str = Depends(verify_and_get_user_id_from_jwt)):
    from core.agents.repo import get_thread_agent_runs as repo_get_thread_runs
    from core.cache.runtime_cache import get_pending_thread
    from core.threads import repo as threads_repo
    
    thread = await threads_repo.get_thread_by_id(thread_id)
    
    if not thread:
        pending = await get_pending_thread(thread_id)
        if pending:
            logger.debug(f"Thread {thread_id} is pending, returning pending agent run")
            return {
                "agent_runs": [{
                    "id": pending.get('agent_run_id'),
                    "status": "running",
                    "started_at": pending.get('created_at'),
                    "completed_at": None
                }] if pending.get('agent_run_id') else [],
                "_pending": True
            }
        raise HTTPException(status_code=404, detail="Thread not found")
    
    client = await db.client
    await verify_and_authorize_thread_access(client, thread_id, user_id)
    agent_runs = await repo_get_thread_runs(thread_id)
    return {"agent_runs": agent_runs}


@router.get("/agent-run/{agent_run_id}", summary="Get Agent Run", operation_id="get_agent_run")
async def get_agent_run(agent_run_id: str, user_id: str = Depends(verify_and_get_user_id_from_jwt)):
    from core.agents.repo import get_agent_run_by_id as repo_get_run
    
    agent_run_data = await repo_get_run(agent_run_id)
    if not agent_run_data:
        raise HTTPException(status_code=404, detail="Agent run not found")
    
    # Auth check
    thread_account_id = agent_run_data.get('thread_account_id')
    metadata = agent_run_data.get('metadata', {}) or {}
    actual_user_id = metadata.get('actual_user_id')
    
    if not (actual_user_id == user_id or thread_account_id == user_id):
        client = await db.client
        await verify_and_authorize_thread_access(client, agent_run_data['thread_id'], user_id)
    
    return {
        "id": agent_run_data['id'],
        "threadId": agent_run_data['thread_id'],
        "status": agent_run_data['status'],
        "startedAt": agent_run_data.get('started_at'),
        "completedAt": agent_run_data.get('completed_at'),
        "error": agent_run_data.get('error')
    }


@router.get("/agent-run/{agent_run_id}/stream", summary="Stream Agent Run", operation_id="stream_agent_run")
async def stream_agent_run(
    agent_run_id: str,
    token: Optional[str] = None,
    last_id: Optional[str] = None,
    request: Request = None
):
    from core.agents import repo as agents_repo
    from core.cache.runtime_cache import get_agent_run_stream_data
    
    user_id = await get_user_id_from_stream_auth(request, token)
    stream_key = f"agent_run:{agent_run_id}:stream"
    
    agent_run_data = await get_agent_run_stream_data(agent_run_id)
    
    if not agent_run_data:
        agent_run_data = await agents_repo.get_agent_run_with_thread(agent_run_id)
    
    if not agent_run_data:
        for attempt in range(15):
            await asyncio.sleep(0.2)
            agent_run_data = await get_agent_run_stream_data(agent_run_id)
            if agent_run_data:
                break

            agent_run_data = await agents_repo.get_agent_run_with_thread(agent_run_id)
            if agent_run_data:
                break
            if attempt >= 5:
                try:
                    stream_len = await redis.stream_len(stream_key)
                    if stream_len > 0:
                        continue
                except Exception:
                    pass
        
        if not agent_run_data:
            raise HTTPException(status_code=404, detail="Worker run not found")
    
    thread_id = agent_run_data['thread_id']
    account_id = agent_run_data['thread_account_id']
    
    if user_id != account_id:
        metadata = agent_run_data.get('metadata', {}) or {}
        shared_users = metadata.get('shared_users', [])
        if user_id not in shared_users:
            raise HTTPException(status_code=403, detail="Access denied")

    def compare_stream_ids(id1: str, id2: str) -> int:
        try:
            t1, s1 = id1.split('-')
            t2, s2 = id2.split('-')
            if int(t1) != int(t2):
                return -1 if int(t1) < int(t2) else 1
            return -1 if int(s1) < int(s2) else (0 if int(s1) == int(s2) else 1)
        except Exception:
            return -1 if id1 < id2 else (0 if id1 == id2 else 1)

    def find_last_safe_boundary(entries):
        last_safe = -1
        open_responses = 0

        for i, (_, fields) in enumerate(entries):
            try:
                data = json.loads(fields.get('data', '{}'))
                msg_type = data.get('type')

                if msg_type == 'llm_response_start':
                    open_responses += 1
                elif msg_type == 'llm_response_end':
                    open_responses = max(0, open_responses - 1)
                    if open_responses == 0:
                        last_safe = i
                elif msg_type == 'status' and data.get('status') in ['completed', 'failed', 'stopped', 'error']:
                    last_safe = i
            except Exception:
                continue

        if open_responses > 0:
            return -1
        return last_safe

    async def stream_generator(agent_run_data, client_last_id: Optional[str] = None):
        terminate = False
        last_id = client_last_id if client_last_id and client_last_id != "0" else "0"
        skip_catchup = client_last_id and client_last_id != "0"

        try:
            # CRITICAL FIX: Subscribe FIRST, then catch-up to avoid race condition
            # The race condition occurs when:
            # 1. Catch-up reads messages 1-5
            # 2. Messages 6-8 are written to Redis during catch-up
            # 3. Hub pump reads and processes 6-8 (no subscribers yet)
            # 4. Subscriber joins AFTER 6-8 were processed
            # 5. Subscriber misses 6-8 forever
            #
            # Fix: Subscribe first so the queue captures new messages while we catch-up
            # Then yield catch-up messages, then process queue (filtering duplicates)
            
            timeout_count = 0
            ping_count = 0
            received_data = False
            MAX_PINGS_WITHOUT_DATA = 4
            catchup_ids = set()  # Track IDs seen during catch-up for deduplication

            try:
                # Subscribe to hub FIRST to capture any new messages
                logger.debug(f"[STREAM] Subscribing to {stream_key} with last_id={last_id}")
                async with redis.redis.hub.subscription(stream_key, last_id) as queue:
                    # NOW do catch-up while subscribed (queue captures concurrent writes)
                    if not skip_catchup:
                        entries = await redis.stream_range(stream_key) or []
                        logger.debug(f"[STREAM] Catch-up found {len(entries)} entries for {stream_key}")
                        if entries:
                            for entry_id, fields in entries:
                                response = json.loads(fields.get('data', '{}'))
                                response['_event_id'] = entry_id
                                yield f"data: {json.dumps(response)}\n\n"
                                last_id = entry_id
                                catchup_ids.add(entry_id)  # Track for deduplication
                                received_data = True
                                if response.get('type') == 'status' and response.get('status') in ['completed', 'failed', 'stopped', 'error']:
                                    return
                            
                            if last_id != "0":
                                try:
                                    safe_idx = find_last_safe_boundary(entries)
                                    if safe_idx >= 0:
                                        safe_id = entries[safe_idx][0]
                                        if '-' in safe_id:
                                            parts = safe_id.split('-')
                                            if len(parts) == 2:
                                                next_id = f"{parts[0]}-{int(parts[1]) + 1}"
                                                await redis.xtrim_minid(stream_key, next_id, approximate=True)
                                except Exception as e:
                                    logger.warning(f"Error in stream catch-up: {e}")

                    if agent_run_data.get('status') != 'running':
                        yield f"data: {json.dumps({'type': 'status', 'status': 'completed'})}\n\n"
                        return

                    logger.debug(f"[STREAM] Starting queue processing for {stream_key}, catchup_ids={len(catchup_ids)}")
                    # Process messages from the queue (including any that arrived during catch-up)
                    async for msg in redis.redis.hub.iter_queue(queue, timeout=0.5):
                        if terminate:
                            break

                        if msg is not None:
                            entry_id, fields = msg
                            # Dedupe: skip if we already saw this in catch-up or earlier
                            if entry_id in catchup_ids or compare_stream_ids(entry_id, last_id) <= 0:
                                continue
                            received_data = True
                            timeout_count = 0
                            ping_count = 0
                            data = fields.get('data', '{}')
                            # Include event ID in response for client tracking
                            try:
                                response = json.loads(data)
                                response['_event_id'] = entry_id
                                yield f"data: {json.dumps(response)}\n\n"
                                if response.get('type') == 'status' and response.get('status') in ['completed', 'failed', 'stopped', 'error']:
                                    return
                            except Exception:
                                yield f"data: {data}\n\n"
                            last_id = entry_id
                        else:
                            # Timeout (0.5s) - send ping every ~5 seconds
                            timeout_count += 1
                            if timeout_count >= 10:
                                timeout_count = 0
                                ping_count += 1

                                # Check for dead worker if no data received
                                if not received_data and ping_count >= MAX_PINGS_WITHOUT_DATA:
                                    from core.agents import repo as agents_repo
                                    run_check = await agents_repo.get_agent_run_status(agent_run_id)
                                    status = run_check.get('status') if run_check else None
                                    if status == 'running':
                                        await agents_repo.update_agent_run_status(agent_run_id, 'failed', error='Worker timeout')
                                        yield f"data: {json.dumps({'type': 'status', 'status': 'error', 'message': 'Worker timeout'})}\n\n"
                                        return
                                    elif status in ['completed', 'failed', 'stopped']:
                                        yield f"data: {json.dumps({'type': 'status', 'status': status})}\n\n"
                                        return

                                yield f"data: {json.dumps({'type': 'ping'})}\n\n"

            except asyncio.CancelledError:
                pass
            except Exception as e:
                yield f"data: {json.dumps({'type': 'status', 'status': 'error', 'message': str(e)})}\n\n"

        except Exception as e:
            logger.error(f"Stream error for {agent_run_id}: {e}", exc_info=True)
            yield f"data: {json.dumps({'type': 'status', 'status': 'error', 'message': str(e)})}\n\n"

    return StreamingResponse(
        stream_generator(agent_run_data, last_id), 
        media_type="text/event-stream", 
        headers={
            "Cache-Control": "no-cache, no-transform", 
            "Connection": "keep-alive",
            "X-Accel-Buffering": "no", 
            "Content-Type": "text/event-stream",
            "Access-Control-Allow-Origin": "*"
        }
    )