File size: 53,873 Bytes
7540aea
 
 
 
 
 
 
df6216c
c663926
3705a49
7540aea
 
 
 
 
3f28951
3705a49
 
 
 
04f258d
3705a49
04f258d
 
 
 
 
 
 
3705a49
04f258d
3705a49
 
04f258d
3705a49
04f258d
 
 
 
 
3705a49
04f258d
 
 
 
 
 
 
 
 
3705a49
 
04f258d
3705a49
 
04f258d
3705a49
04f258d
 
 
3705a49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7540aea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c663926
 
 
 
 
7540aea
 
 
 
b8ba5a3
aa5ee91
0f13d1d
 
 
 
b8ba5a3
d2fe867
b8ba5a3
7540aea
 
 
 
a51697c
c663926
04f258d
a51697c
 
 
04f258d
a51697c
 
 
04f258d
 
 
 
 
 
 
 
a51697c
04f258d
a51697c
 
 
04f258d
a51697c
 
04f258d
 
 
 
 
 
 
 
 
 
 
 
a51697c
 
 
04f258d
a51697c
 
 
 
 
 
04f258d
 
 
a51697c
 
9427d4d
 
c663926
a51697c
7540aea
 
a51697c
 
 
7540aea
 
a51697c
 
7540aea
 
 
a51697c
7540aea
 
 
04f258d
7540aea
 
 
 
 
04f258d
 
7540aea
 
a51697c
7540aea
 
a51697c
 
7540aea
 
 
a51697c
7540aea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9791d99
 
9eb7321
 
 
 
7540aea
 
 
df6216c
 
 
 
 
9eb7321
b423b58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37d6c24
 
 
 
 
 
 
 
 
7540aea
 
 
 
 
 
 
 
 
 
 
04f258d
 
 
 
 
 
 
 
7540aea
 
 
 
 
 
 
 
 
b423b58
5d6ab9e
 
04f258d
 
7540aea
 
 
 
04f258d
 
 
 
 
ea87ddf
 
 
 
064d9d1
ec449f9
 
 
 
d2fe867
7540aea
 
 
aa5ee91
 
 
 
b8ba5a3
 
 
aa5ee91
 
b8ba5a3
aa5ee91
b8ba5a3
 
aa5ee91
 
 
 
 
 
 
 
 
 
 
0f13d1d
aa5ee91
0f13d1d
 
aa5ee91
 
0f13d1d
aa5ee91
d2fe867
aa5ee91
 
b8ba5a3
 
 
 
 
 
0f13d1d
aa5ee91
 
0f13d1d
aa5ee91
 
d2fe867
aa5ee91
d2fe867
aa5ee91
 
 
 
0f13d1d
aa5ee91
0f13d1d
 
aa5ee91
 
d2fe867
aa5ee91
d2fe867
aa5ee91
 
 
b8ba5a3
aa5ee91
 
b8ba5a3
 
 
bea12d2
 
 
3705a49
 
 
 
 
 
 
 
 
 
 
c663926
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3705a49
 
 
 
37d6c24
3705a49
 
c663926
3705a49
 
 
 
 
aa5ee91
3705a49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea12d2
 
 
 
 
 
7540aea
aa5ee91
b8ba5a3
7540aea
 
 
 
 
9791d99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7540aea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d6ab9e
 
7540aea
 
 
 
 
 
 
df6216c
 
 
7540aea
 
 
 
 
 
 
 
 
9eb7321
df6216c
 
 
 
9eb7321
df6216c
 
 
9eb7321
df6216c
 
9eb7321
df6216c
9eb7321
df6216c
9eb7321
 
 
 
 
 
 
 
 
 
7540aea
 
9791d99
 
 
 
7540aea
 
 
4d96cf9
7540aea
 
4d96cf9
33e6f65
 
 
 
 
 
 
 
 
 
7540aea
 
 
 
 
 
 
 
 
 
 
4d96cf9
 
 
 
 
9791d99
 
 
 
 
 
 
 
7540aea
 
9791d99
 
7540aea
9791d99
 
 
 
 
 
 
 
 
 
 
 
7540aea
9791d99
 
 
 
7540aea
9791d99
 
 
 
 
 
 
 
 
 
 
 
7540aea
 
df6216c
 
 
f3c622e
7540aea
 
 
 
 
 
 
 
 
9791d99
5d6ab9e
 
 
 
 
 
 
 
7540aea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df6216c
 
5d6ab9e
9791d99
3705a49
 
 
37d6c24
3705a49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c663926
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3705a49
 
 
37d6c24
 
3705a49
 
4d96cf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3705a49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7540aea
 
 
 
3705a49
7540aea
 
9791d99
 
 
 
7540aea
9791d99
7540aea
9791d99
 
 
7540aea
 
 
 
 
3f28951
7540aea
 
37d6c24
 
 
 
 
 
 
 
7540aea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3c622e
 
 
 
 
 
 
7540aea
 
 
 
 
 
 
 
 
 
 
 
04f258d
 
 
 
 
 
7540aea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38797d2
 
 
 
 
 
7540aea
 
 
 
 
 
 
4d96cf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9791d99
7540aea
9791d99
7540aea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Claw Agent Runtime β€” the core agentic conversation loop.
 * Handles streaming responses, tool calls, and multi-turn conversations.
 */

import { ENV } from "../_core/env";
import { buildSystemPrompt, TOOL_DEFINITIONS } from "./system-prompt";
import { executeTool, getPlanMode, runPreToolHooks, runPostToolHooks, initializeMcpFromConfig, getMcpManager } from "../tools/executor";
import { compactSession, compactSessionWithLLM, shouldCompact, estimateSessionTokens, dbMessagesToSession, DEFAULT_COMPACTION_CONFIG } from "./compact";
import type { Session, ConversationMessage as CompactMessage, CompactionConfig } from "./compact";
import { UsageTracker, pricingForModel, defaultSonnetTierPricing, estimateCostUsdWithPricing, totalCostUsd, formatUsd, summaryLinesForModel } from "./usage";
import type { TokenUsage } from "./usage";
import type { Response } from "express";
import { execSync } from "child_process";

// In original claw-code, max_iterations defaults to usize::MAX (effectively unlimited).
// Auto-compact is triggered on context overflow (400 error) β€” matches original compact() method.

// Context window sizes for known models (used for proactive compaction)
const MODEL_CONTEXT_WINDOWS: Record<string, number> = {
  // Xiaomi MiMo
  "XiaomiMiMo/MiMo-V2-Flash": 262144,
  // Qwen models (DeepInfra + HuggingFace)
  "Qwen/Qwen3-Coder-480B-A35B-Instruct-Turbo": 262144,
  "Qwen/Qwen3-Coder-480B-A35B-Instruct": 262144,
  "Qwen/Qwen3-235B-A22B-Instruct-2507": 262144,
  "Qwen/Qwen3-235B-A22B-Thinking-2507": 262144,
  "Qwen/Qwen3.5-397B-A17B": 262144,
  "Qwen/Qwen3.5-122B-A10B": 262144,
  "Qwen/Qwen3-Coder-Next": 131072,
  "Qwen/Qwen3-32B": 40960,
  "Qwen/Qwen3-8B": 32768,
  "Qwen/Qwen3-Coder-30B-A3B-Instruct": 131072,
  // Meta Llama
  "meta-llama/Llama-3.3-70B-Instruct": 131072,
  "meta-llama/Llama-4-Maverick-17B-128E": 1048576,
  "meta-llama/Llama-4-Scout-17B-16E": 327680,
  // DeepSeek
  "deepseek-ai/DeepSeek-V3.2": 163840,
  "deepseek-ai/DeepSeek-V3.1": 163840,
  "deepseek-ai/DeepSeek-R1": 131072,
  "deepseek-ai/DeepSeek-R1-0528": 163840,
  // NVIDIA Nemotron
  "nvidia/NVIDIA-Nemotron-3-Super-120B-A12B": 262144,
  // StepFun
  "stepfun-ai/Step-3.5-Flash": 262144,
  // NousResearch (uncensored)
  "NousResearch/Hermes-3-Llama-3.1-70B": 131072,
  "NousResearch/Hermes-3-Llama-3.1-405B": 131072,
  // Anthropic
  "claude-opus-4-6": 200000,
  "claude-sonnet-4-6": 200000,
  // OpenAI
  "gpt-5.4": 1048576,
  "gpt-4.1": 1048576,
  // xAI
  "grok-3": 131072,
  // Google
  "google/gemini-2.5-flash": 1000000,
  "google/gemini-2.5-pro": 1000000,
};

const DEFAULT_CONTEXT_WINDOW = 131072;

/**
 * Convert agent messages to compact.ts Session format for compaction.
 */
function agentMessagesToSession(messages: AgentMessage[]): Session {
  return dbMessagesToSession(
    messages.map((m) => ({
      role: m.role,
      content: m.content || "",
      toolName: m.name || null,
      toolCallId: m.tool_call_id || null,
    }))
  );
}

/**
 * Convert compacted Session back to AgentMessage[] format.
 */
function sessionToAgentMessages(session: Session): AgentMessage[] {
  return session.messages.map((msg) => {
    const agentMsg: AgentMessage = {
      role: msg.role,
      content: msg.blocks
        .filter((b) => b.type === "text")
        .map((b) => b.text || "")
        .join("\n") || null,
    };
    // Reconstruct tool_calls from tool_use blocks
    const toolUseBlocks = msg.blocks.filter((b) => b.type === "tool_use");
    if (toolUseBlocks.length > 0) {
      agentMsg.tool_calls = toolUseBlocks.map((b, i) => ({
        id: `compacted_${i}_${Date.now()}`,
        type: "function" as const,
        function: {
          name: b.name || "unknown",
          arguments: b.input || "{}",
        },
      }));
    }
    // Reconstruct tool result fields
    const toolResultBlock = msg.blocks.find((b) => b.type === "tool_result");
    if (toolResultBlock) {
      agentMsg.name = toolResultBlock.toolName;
      agentMsg.content = toolResultBlock.output || "";
    }
    return agentMsg;
  });
}

/**
 * Estimate total tokens in the conversation (simple heuristic: ~4 chars per token).
 */
function estimateConversationTokens(messages: AgentMessage[]): number {
  let total = 0;
  for (const msg of messages) {
    total += Math.ceil((msg.content?.length || 0) / 4) + 4; // +4 for role/overhead
    if (msg.tool_calls) {
      for (const tc of msg.tool_calls) {
        total += Math.ceil((tc.function.name.length + tc.function.arguments.length) / 4) + 4;
      }
    }
  }
  return total;
}

interface AgentMessage {
  role: "system" | "user" | "assistant" | "tool";
  content: string | null;
  tool_calls?: Array<{
    id: string;
    type: "function";
    function: { name: string; arguments: string };
  }>;
  tool_call_id?: string;
  name?: string;
}

interface AgentConfig {
  model: string;
  apiProvider: string;
  apiKey?: string | null;
  apiBaseUrl?: string | null;
  maxTokens: number;
  temperature: number;
  topP: number;
  systemPrompt?: string | null;
  memory?: string | null;
  workDir?: string;
  effortLevel?: "low" | "medium" | "high";
  maxIterations?: number;
}

/**
 * TurnSummary β€” matches original conversation.rs TurnSummary struct.
 * Returned after each complete agent turn.
 */
export interface TurnSummary {
  assistantMessages: AgentMessage[];
  toolResults: AgentMessage[];
  iterations: number;
  usage: TokenUsage;
}

/**
 * Read git status (matches original read_git_status from prompt.rs)
 */
function readGitStatus(cwd: string): string | null {
  try {
    const output = execSync("git --no-optional-locks status --short --branch", {
      cwd,
      timeout: 5000,
      encoding: "utf-8",
      stdio: ["pipe", "pipe", "pipe"],
    }).trim();
    return output || null;
  } catch {
    return null;
  }
}

/**
 * Read git diff (matches original read_git_diff from prompt.rs)
 */
function readGitDiff(cwd: string): string | null {
  try {
    const sections: string[] = [];
    try {
      const staged = execSync("git diff --cached", {
        cwd, timeout: 5000, encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"],
      }).trim();
      if (staged) sections.push(`Staged changes:\n${staged}`);
    } catch {}
    try {
      const unstaged = execSync("git diff", {
        cwd, timeout: 5000, encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"],
      }).trim();
      if (unstaged) sections.push(`Unstaged changes:\n${unstaged}`);
    } catch {}
    return sections.length > 0 ? sections.join("\n\n") : null;
  } catch {
    return null;
  }
}

/**
 * Merge hook feedback into tool output β€” matches original merge_hook_feedback()
 */
function mergeHookFeedback(hookMessages: string[], output: string, denied: boolean): string {
  if (hookMessages.length === 0) return output;
  const sections: string[] = [];
  if (output.trim()) sections.push(output);
  const label = denied ? "Hook feedback (denied)" : "Hook feedback";
  sections.push(`${label}:\n${hookMessages.join("\n")}`);
  return sections.join("\n\n");
}

const DEFAULT_CONFIG: AgentConfig = {
  model: process.env.DEFAULT_MODEL || "Qwen/Qwen3-Coder-480B-A35B-Instruct-Turbo",
  apiProvider: "deepinfra",
  maxTokens: 32768,       // Qwen3-Coder supports up to 65k output
  temperature: 0.5,       // Lower temp = more focused/deterministic agent behavior
  topP: 0.95,             // Slightly restricted for more coherent tool calls
  workDir: process.env.WORKSPACE_DIR || "/home/ubuntu",
  effortLevel: "high",
};

/**
 * Retry config for transient API errors.
 * - 429 (rate limit): retry INFINITELY every 2 seconds until it works.
 * - 500/502/503 (server errors): retry INFINITELY every 2 seconds.
 * - Network errors: retry INFINITELY every 2 seconds.
 * We NEVER give up on transient errors β€” just keep trying.
 */
const RETRY_DELAY_MS = 2000; // fixed 2 second interval β€” simple and reliable

/**
 * Resolve the API URL and key based on provider config
 */
function resolveApiConfig(config: AgentConfig) {
  // ─── HARDCODED FALLBACK β€” always works even if settings are corrupted ───
  const FALLBACK_URL = "https://api.deepinfra.com/v1/openai";
  const FALLBACK_MODEL = "Qwen/Qwen3-Coder-480B-A35B-Instruct-Turbo";

  // Resolve model aliases (used for both default and custom paths)
  const aliasMap: Record<string, string> = {
    // Xiaomi MiMo
    mimo: "XiaomiMiMo/MiMo-V2-Flash",
    "mimo-flash": "XiaomiMiMo/MiMo-V2-Flash",
    "mimo-v2": "XiaomiMiMo/MiMo-V2-Flash",
    // Qwen models (DeepInfra)
    "qwen-coder": "Qwen/Qwen3-Coder-480B-A35B-Instruct-Turbo",
    "qwen-coder-turbo": "Qwen/Qwen3-Coder-480B-A35B-Instruct-Turbo",
    "qwen-coder-480b": "Qwen/Qwen3-Coder-480B-A35B-Instruct",
    "qwen3-235b": "Qwen/Qwen3-235B-A22B-Instruct-2507",
    "qwen3-thinking": "Qwen/Qwen3-235B-A22B-Thinking-2507",
    "qwen3.5": "Qwen/Qwen3.5-397B-A17B",
    "qwen3-32b": "Qwen/Qwen3-32B",
    "qwen3-8b": "Qwen/Qwen3-8B",
    "qwen3-coder": "Qwen/Qwen3-Coder-480B-A35B-Instruct-Turbo",
    // Llama
    llama: "meta-llama/Llama-3.3-70B-Instruct",
    "llama-70b": "meta-llama/Llama-3.3-70B-Instruct",
    "llama-4": "meta-llama/Llama-4-Maverick-17B-128E",
    // DeepSeek
    deepseek: "deepseek-ai/DeepSeek-V3.2",
    "deepseek-r1": "deepseek-ai/DeepSeek-R1-0528",
    "deepseek-v3": "deepseek-ai/DeepSeek-V3.2",
    // NVIDIA
    nemotron: "nvidia/NVIDIA-Nemotron-3-Super-120B-A12B",
    // StepFun
    step: "stepfun-ai/Step-3.5-Flash",
    "step-flash": "stepfun-ai/Step-3.5-Flash",
    // Uncensored
    hermes: "NousResearch/Hermes-3-Llama-3.1-70B",
    "hermes-405b": "NousResearch/Hermes-3-Llama-3.1-405B",
    uncensored: "NousResearch/Hermes-3-Llama-3.1-70B",
    // OpenAI GPT-5.x family
    "gpt5": "gpt-5.4",
    "gpt-5": "gpt-5.4",
    "gpt54": "gpt-5.4",
    // Anthropic aliases
    opus: "claude-opus-4-6",
    sonnet: "claude-sonnet-4-6",
    haiku: "claude-haiku-4-5-20251213",
    // xAI
    grok: "grok-3",
    "grok-3": "grok-3",
    // Google
    gemini: "google/gemini-2.5-flash",
    "gemini-pro": "google/gemini-2.5-pro",
  };

  // Treat empty, null, masked, or built-in providers as "use server default"
  const hasCustomKey = config.apiKey && config.apiKey.length > 4 && !config.apiKey.startsWith("β€’β€’β€’β€’");
  if (config.apiProvider === "claw" || config.apiProvider === "default" || config.apiProvider === "huggingface" || config.apiProvider === "deepinfra" || !hasCustomKey) {
    const defaultModel = process.env.DEFAULT_MODEL || FALLBACK_MODEL;
    const resolvedModel = aliasMap[config.model] || config.model || defaultModel;
    // Use BUILT_IN_FORGE_API_URL from env β€” HuggingFace router or OpenAI
    const baseUrl = (ENV.forgeApiUrl || FALLBACK_URL).replace(/\/$/, "");
    const apiKey = ENV.forgeApiKey || process.env.BUILT_IN_FORGE_API_KEY || "";
    console.log(`[agent] resolveApiConfig: using server default. URL=${baseUrl}, model=${resolvedModel}, hasKey=${!!apiKey}`);
    return {
      url: `${baseUrl}/chat/completions`,
      key: apiKey,
      model: resolvedModel || FALLBACK_MODEL,
    };
  }

  // Custom provider path β€” user has their own API key
  let baseUrl = config.apiBaseUrl || "";
  if (!baseUrl) {
    const providers: Record<string, string> = {
      deepinfra: "https://api.deepinfra.com/v1/openai",
      huggingface: "https://router.huggingface.co/v1",
      xai: "https://api.x.ai/v1",
      openrouter: "https://openrouter.ai/api/v1",
      openai: "https://api.openai.com/v1",
      anthropic: "https://api.anthropic.com/v1",
      groq: "https://api.groq.com/openai/v1",
      cerebras: "https://api.cerebras.ai/v1",
      ollama: "http://localhost:11434/v1",
    };
    baseUrl = providers[config.apiProvider] || FALLBACK_URL;
  }

  const resolvedModel = aliasMap[config.model] || config.model || FALLBACK_MODEL;
  console.log(`[agent] resolveApiConfig: custom provider. URL=${baseUrl}, model=${resolvedModel}`);
  return {
    url: `${baseUrl.replace(/\/$/, "")}/chat/completions`,
    key: config.apiKey,
    model: resolvedModel,
  };
}

/**
 * Send an SSE event to the client
 */
function sendSSE(res: Response, event: string, data: unknown) {
  try {
    res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
  } catch {
    // Connection may be closed
  }
}

/**
 * Run the agentic loop: send messages to LLM, execute tool calls, repeat.
 * This is the core of the agent β€” it loops until the LLM stops calling tools.
 */
export async function runAgentLoop(
  messages: AgentMessage[],
  sessionId: number,
  config: Partial<AgentConfig>,
  res: Response,
  signal?: AbortSignal
): Promise<{
  finalMessages: AgentMessage[];
  totalPromptTokens: number;
  totalCompletionTokens: number;
  totalCost: number;
  model: string;
}> {
  const cfg = { ...DEFAULT_CONFIG, ...config };
  const apiConfig = resolveApiConfig(cfg);
  const workDir = cfg.workDir || "/home/ubuntu";

  // Get plan mode state
  const planState = getPlanMode(sessionId);

  // Read git status and diff (matches original ProjectContext::discover_with_git)
  const gitStatus = readGitStatus(workDir);
  const gitDiff = readGitDiff(workDir);

  // Build system prompt with full environment context
  const systemPrompt = buildSystemPrompt({
    memory: cfg.memory,
    effortLevel: cfg.effortLevel || "high",
    planMode: planState.active,
    planSteps: planState.steps,
    customSystemPrompt: cfg.systemPrompt,
    workDir,
    platform: "linux",
    model: apiConfig.model,
    gitStatus,
    gitDiff,
  });

  // Initialize UsageTracker (matches original conversation.rs)
  const usageTracker = UsageTracker.new();

  // Build conversation with system message first
  const conversationMessages: AgentMessage[] = [
    { role: "system", content: systemPrompt },
    ...messages.filter((m) => m.role !== "system"),
  ];

  let totalPromptTokens = 0;
  let totalCompletionTokens = 0;
  let totalCost = 0;
  let iterations = 0;
  let emptyResponseRetries = 0;
  const MAX_EMPTY_RETRIES = 3;
  // Safety limit: prevent infinite loops. Original claw-code uses usize::MAX but that
  // causes runaway loops with Qwen3 which sometimes fails to stop generating.
  // 200 iterations is more than enough for any real task.
  const MAX_ITERATIONS = cfg.maxIterations || 200;
  const assistantMessages: AgentMessage[] = [];
  const toolResultMessages: AgentMessage[] = [];

  // ─── Loop detection: minimal safety net ─────────────────────────────
  // Only detect EXACT same tool+args repeated 5+ times (true infinite loop).
  // Everything else is handled by MAX_ITERATIONS.
  const recentToolSignatures: string[] = [];
  const MAX_EXACT_REPEATS = 5;

  // ─── MCP Tools Dynamic Injection (matches original claw-code) ──────────
  // Initialize MCP servers from config and merge discovered tools with static TOOL_DEFINITIONS.
  // This is how the original claw-code dynamically builds the tool list:
  //   1. Load MCP server configs from .claw/settings.json
  //   2. Connect to each server via stdio JSON-RPC
  //   3. Call tools/list to discover available tools
  //   4. Prefix tool names as mcp__servername__toolname
  //   5. Merge with static tool definitions
  let allTools = [...TOOL_DEFINITIONS];
  try {
    const mcpTools = await initializeMcpFromConfig(workDir);
    if (mcpTools.length > 0) {
      const mcpManager = getMcpManager();
      if (mcpManager) {
        const mcpDefs = mcpManager.getToolDefinitions();
        // Convert MCP tool format to OpenAI function calling format
        const mcpToolDefs = mcpDefs.map((t) => ({
          type: "function" as const,
          function: {
            name: t.name,
            description: t.description,
            parameters: t.input_schema || { type: "object", properties: {} },
          },
        }));
        allTools = [...TOOL_DEFINITIONS, ...mcpToolDefs];
        console.log(`[agent] MCP tools injected: ${mcpDefs.map((t) => t.name).join(", ")}`);
        sendSSE(res, "status", {
          status: "mcp_ready",
          message: `MCP tools loaded: ${mcpDefs.length} tools from ${mcpManager.getConnectedServers().length} servers`,
        });
      }
    }
  } catch (err: any) {
    console.error(`[agent] MCP initialization error (non-fatal):`, err.message);
    // MCP init failure is non-fatal β€” agent continues with static tools only
  }

  // ─── Context-aware compaction config ──────────────────────────────
  // Original claw-code uses percentage-based thresholds, not fixed 10k tokens.
  // We compute the threshold as 70% of the model's context window.
  const contextWindow = MODEL_CONTEXT_WINDOWS[apiConfig.model] || DEFAULT_CONTEXT_WINDOW;
  const dynamicCompactionConfig: import("./compact").CompactionConfig = {
    preserveRecentMessages: DEFAULT_COMPACTION_CONFIG.preserveRecentMessages,
    maxEstimatedTokens: Math.floor(contextWindow * 0.7),
  };

  sendSSE(res, "status", { status: "thinking", message: "Processing your request..." });

  while (iterations < MAX_ITERATIONS) {
    iterations++;

    if (signal?.aborted) {
      sendSSE(res, "status", { status: "cancelled", message: "Request cancelled" });
      break;
    }

    // Build API request
    // Determine max_tokens limit based on provider
    const isDeepInfra = apiConfig.url.includes("deepinfra.com");
    const isHuggingFace = apiConfig.url.includes("huggingface.co");
    const maxTokensLimit = isHuggingFace ? 32000 : (isDeepInfra ? 65536 : 65536);

    // Detect if model supports thinking/reasoning mode (Qwen3 Thinking, DeepSeek-R1)
    const isThinkingModel = apiConfig.model.includes("Thinking") || apiConfig.model.includes("R1");

    const payload: Record<string, unknown> = {
      model: apiConfig.model,
      messages: conversationMessages.map((m) => {
        const msg: Record<string, unknown> = { role: m.role, content: m.content };
        if (m.tool_calls) msg.tool_calls = m.tool_calls;
        if (m.tool_call_id) msg.tool_call_id = m.tool_call_id;
        if (m.name) msg.name = m.name;
        return msg;
      }),
      tools: allTools,
      // Match original: always "auto" β€” let the model decide what tools to use
      tool_choice: "auto",
      max_tokens: Math.min(cfg.maxTokens, maxTokensLimit),
      temperature: isThinkingModel ? 0.6 : cfg.temperature, // thinking models need lower temp
      top_p: cfg.topP,
      stream: true,
    };

    // Enable thinking/reasoning for supported models (Qwen3 Thinking, DeepSeek-R1)
    if (isThinkingModel && isDeepInfra) {
      (payload as any).extra_body = { enable_thinking: true };
    }

    // Qwen3-Coder: hybrid thinking mode β€” model decides when to think
    // DO NOT disable thinking; Qwen3 automatically uses <think> blocks for complex tasks
    // The /no_think in system prompt is only a soft hint for simple responses,
    // the model will override it when it needs to reason through tool calls or code

    // NOTE: Prefill technique (empty assistant message) was removed because it caused
    // the model to "talk to itself" β€” seeing an empty assistant message made it think
    // it already responded and continue generating follow-up messages in a loop.
    // Anti-censorship is handled purely via system prompt directives instead.

    sendSSE(res, "message_start", { iteration: iterations });

    try {
      // ─── Retry loop with INFINITE retries for 429, limited for 5xx ─────
      // 429 = rate limit: the API is telling us to slow down, NOT to give up.
      // We MUST keep retrying with increasing backoff until it works.
      // 500/502/503 = server errors: retry up to MAX_SERVER_RETRIES times.
      let response: globalThis.Response | null = null;
      let lastErrorText = "";
      let lastStatus = 0;
      let serverRetryCount = 0;
      let rateLimitRetryCount = 0;

      while (true) {
        if (signal?.aborted) break;

        try {
          response = await fetch(apiConfig.url, {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
              Authorization: `Bearer ${apiConfig.key}`,
            },
            body: JSON.stringify(payload),
            signal,
          });
        } catch (fetchErr: any) {
          // Network error (DNS, connection refused, etc.) β€” retry every 2s
          if (signal?.aborted) break;
          serverRetryCount++;
          console.error(`[agent] Fetch error (retry #${serverRetryCount}):`, fetchErr.message);
          sendSSE(res, "status", {
            status: "retrying",
            message: `Network error, retrying in 2s... (attempt #${serverRetryCount})`,
          });
          await new Promise((r) => setTimeout(r, RETRY_DELAY_MS));
          continue;
        }

        if (response.ok) break;

        lastStatus = response.status;
        lastErrorText = await response.text();

        // ─── 429 Rate Limit: INFINITE retry every 2s ───
        if (response.status === 429) {
          rateLimitRetryCount++;
          console.log(`[agent] Rate limited (429) β€” retry #${rateLimitRetryCount} in 2s`);
          sendSSE(res, "status", {
            status: "rate_limited",
            message: `Rate limited by API β€” retrying in 2s... (attempt #${rateLimitRetryCount})`,
          });
          await new Promise((r) => setTimeout(r, RETRY_DELAY_MS));
          response = null;
          continue; // NEVER give up on 429
        }

        // ─── 500/502/503 Server errors: INFINITE retry every 2s ───
        if ([500, 502, 503].includes(response.status)) {
          serverRetryCount++;
          console.log(`[agent] Server error ${response.status} β€” retry #${serverRetryCount} in 2s`);
          sendSSE(res, "status", {
            status: "retrying",
            message: `Server error ${response.status}, retrying in 2s... (attempt #${serverRetryCount})`,
          });
          await new Promise((r) => setTimeout(r, RETRY_DELAY_MS));
          response = null;
          continue;
        }

        // Any other error (400, 401, 403, 404, etc.) β€” don't retry
        break;
      }

      if (!response || !response.ok) {
        console.error(`[agent] API error ${lastStatus}:`, lastErrorText);
        console.error(`[agent] Payload model:`, apiConfig.model);
        console.error(`[agent] Payload messages count:`, (payload.messages as any[]).length);

        // ─── AUTO-COMPACT on context overflow (400 error) ─────────────
        if (lastStatus === 400 && (lastErrorText.includes("context_length") || lastErrorText.includes("too many tokens") || lastErrorText.includes("maximum context") || lastErrorText.includes("token limit") || lastErrorText.includes("too long"))) {
          console.log(`[agent] Context overflow detected β€” auto-compacting conversation...`);
          sendSSE(res, "status", {
            status: "compacting",
            message: "Context window exceeded β€” auto-compacting conversation...",
          });

          try {
            const session = agentMessagesToSession(conversationMessages);

            // LLM-based summarization: use the same API to produce a real summary
            const llmFetch = async (msgs: Array<{role: string; content: string}>) => {
              const summaryResp = await fetch(apiConfig.url, {
                method: "POST",
                headers: { "Content-Type": "application/json", Authorization: `Bearer ${apiConfig.key}` },
                body: JSON.stringify({
                  model: apiConfig.model,
                  messages: msgs,
                  max_tokens: 2000,
                  temperature: 0.3,
                  stream: false,
                }),
              });
              if (!summaryResp.ok) throw new Error(`LLM summary failed: ${summaryResp.status}`);
              const json = await summaryResp.json();
              return json.choices?.[0]?.message?.content || "";
            };

            const compactResult = await compactSessionWithLLM(session, dynamicCompactionConfig, llmFetch);

            if (compactResult.removedMessageCount > 0) {
              const compactedAgentMessages = sessionToAgentMessages(compactResult.compactedSession);
              conversationMessages.length = 0;
              conversationMessages.push({ role: "system", content: systemPrompt });
              conversationMessages.push(...compactedAgentMessages);

              console.log(`[agent] Auto-compact (LLM): removed ${compactResult.removedMessageCount} messages, kept ${conversationMessages.length}`);
              sendSSE(res, "auto_compact", {
                removedCount: compactResult.removedMessageCount,
                keptCount: conversationMessages.length,
                summary: compactResult.formattedSummary,
              });
              continue; // retry with compacted context
            } else {
              console.error(`[agent] Auto-compact produced no reduction β€” breaking`);
              sendSSE(res, "error", {
                message: `Context overflow but compaction couldn't reduce further`,
                details: lastErrorText,
              });
              break;
            }
          } catch (compactErr: any) {
            console.error(`[agent] Auto-compact failed:`, compactErr.message);
            sendSSE(res, "error", {
              message: `Context overflow β€” auto-compact failed: ${compactErr.message}`,
              details: lastErrorText,
            });
            break;
          }
        }

        // Non-context 400 errors β€” log details for debugging
        if (lastStatus === 400) {
          console.error(`[agent] Full error body:`, lastErrorText);
          (payload.messages as any[]).forEach((m: any, i: number) => {
            console.error(`[agent] msg[${i}] role=${m.role} content_type=${typeof m.content} content_len=${String(m.content || '').length} has_tool_calls=${!!m.tool_calls} has_tool_call_id=${!!m.tool_call_id}`);
          });
        }
        sendSSE(res, "error", {
          message: `API error: ${lastStatus}${lastStatus === 429 ? ' (rate limit)' : ''} β€” ${lastErrorText.substring(0, 200)}`,
          details: lastErrorText,
        });
        break;
      }

      // Process streaming response
      let result: { content: string; toolCalls: Array<{ id: string; type: "function"; function: { name: string; arguments: string } }>; usage?: any };
      try {
        result = await processStream(response, res, signal);
      } catch (streamErr: any) {
        // Stream processing error β€” treat as transient, retry
        console.error(`[agent] Stream processing error:`, streamErr.message);
        if (emptyResponseRetries++ < MAX_EMPTY_RETRIES) {
          sendSSE(res, "status", { status: "retrying", message: `Stream error, retrying... (${emptyResponseRetries}/${MAX_EMPTY_RETRIES})` });
          await new Promise(r => setTimeout(r, 1500));
          continue;
        }
        sendSSE(res, "error", { message: `Stream failed after ${MAX_EMPTY_RETRIES} retries: ${streamErr.message}` });
        break;
      }

      // ─── Bug #1 fix: Handle empty LLM response with retry ─────────
      // Original claw-code retries on empty response instead of crashing.
      // Open-source models via HuggingFace often return empty streams.
      if (!result.content && result.toolCalls.length === 0) {
        if (emptyResponseRetries++ < MAX_EMPTY_RETRIES) {
          console.warn(`[agent] Empty response from LLM β€” retry ${emptyResponseRetries}/${MAX_EMPTY_RETRIES}`);
          sendSSE(res, "status", { status: "retrying", message: `Empty response from model, retrying... (${emptyResponseRetries}/${MAX_EMPTY_RETRIES})` });
          await new Promise(r => setTimeout(r, 1500));
          continue; // retry same iteration
        }
        console.error(`[agent] LLM returned empty response ${MAX_EMPTY_RETRIES} times β€” giving up`);
        sendSSE(res, "error", { message: `Model returned empty response after ${MAX_EMPTY_RETRIES} retries. Try a different model or reduce context.` });
        break;
      }
      emptyResponseRetries = 0; // reset on successful response

      // Track usage with UsageTracker (matches original)
      if (result.usage) {
        totalPromptTokens += result.usage.prompt_tokens || 0;
        totalCompletionTokens += result.usage.completion_tokens || 0;
        usageTracker.record({
          input_tokens: result.usage.prompt_tokens || 0,
          output_tokens: result.usage.completion_tokens || 0,
          cache_creation_input_tokens: result.usage.cache_creation_input_tokens || 0,
          cache_read_input_tokens: result.usage.cache_read_input_tokens || 0,
        });
      }

      // Add assistant message to conversation
      const assistantMessage: AgentMessage = {
        role: "assistant",
        // Match original: null when no content (even with tool_calls)
        content: result.content || null,
      };
      if (result.toolCalls && result.toolCalls.length > 0) {
        assistantMessage.tool_calls = result.toolCalls;
      }
      conversationMessages.push(assistantMessage);
      assistantMessages.push(assistantMessage);

      // If no tool calls, we're done β€” the LLM has finished responding.
      // This matches the original claw-code behavior exactly:
      // the model decides when to stop by not calling tools.
      if (!result.toolCalls || result.toolCalls.length === 0) {
        sendSSE(res, "message_end", {
          promptTokens: totalPromptTokens,
          completionTokens: totalCompletionTokens,
          cost: totalCost,
          model: apiConfig.model,
        });
        break;
      }

      // ─── Minimal loop detection: only catch TRUE infinite loops ───────
      // Only break if the EXACT same tool+args is repeated 5+ times.
      // This is the only safety net beyond MAX_ITERATIONS.
      // The original claw-code has NO loop detection at all β€” it trusts the model.
      const currentToolSig = result.toolCalls.map((tc: any) => `${tc.function.name}:${tc.function.arguments}`).join("|");
      recentToolSignatures.push(currentToolSig);
      if (recentToolSignatures.length > MAX_EXACT_REPEATS) {
        recentToolSignatures.shift();
      }
      if (recentToolSignatures.length >= MAX_EXACT_REPEATS) {
        const allSame = recentToolSignatures.every(r => r === recentToolSignatures[0]);
        if (allSame) {
          console.warn(`[agent] Infinite loop detected: exact same tool call repeated ${MAX_EXACT_REPEATS} times β€” breaking`);
          sendSSE(res, "error", {
            message: `⚠️ ΠΎΠ±Π½Π°Ρ€ΡƒΠΆΠ΅Π½ бСсконСчный Ρ†ΠΈΠΊΠ». ΠΏΠΎΠΏΡ€ΠΎΠ±ΡƒΠΉ ΠΏΠ΅Ρ€Π΅Ρ„ΠΎΡ€ΠΌΡƒΠ»ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ запрос`,
          });
          sendSSE(res, "message_end", {
            promptTokens: totalPromptTokens,
            completionTokens: totalCompletionTokens,
            cost: totalCost,
            model: apiConfig.model,
          });
          break;
        }
      }

      // ─── Execute tool calls ──────────────────────────────────────────
      // Bug #2+#3 fix: Each tool call is wrapped in its own try-catch.
      // Original claw-code sends tool errors back to LLM as tool results,
      // letting the model decide how to handle them. We NEVER break the
      // loop on a tool error β€” only on fatal API/stream errors.
      for (const toolCall of result.toolCalls) {
        const toolName = toolCall.function.name;
        let toolArgs: Record<string, unknown> = {};
        let argParseError = false;
        try {
          toolArgs = JSON.parse(toolCall.function.arguments || "{}");
        } catch (parseErr: any) {
          // Try JSON repair before giving up
          try {
            const { jsonrepair } = await import("jsonrepair");
            const repaired = jsonrepair(toolCall.function.arguments || "{}");
            toolArgs = JSON.parse(repaired);
            console.info(`[agent] Repaired malformed JSON for ${toolName}`);
          } catch (repairErr: any) {
            argParseError = true;
            console.warn(`[agent] Malformed tool args for ${toolName} (repair failed):`, toolCall.function.arguments?.substring(0, 200));
          }
        }

        sendSSE(res, "tool_call_start", {
          id: toolCall.id,
          name: toolName,
          arguments: toolCall.function.arguments,
        });

        let toolOutput: string;
        let isError = false;

        // If JSON args were malformed, skip execution and tell LLM to fix
        if (argParseError) {
          toolOutput = `Error: Your tool call arguments for '${toolName}' contained malformed JSON. The raw arguments were: ${(toolCall.function.arguments || "").substring(0, 500)}. Please fix the JSON and try again.`;
          isError = true;
        } else try {
          // ─── Pre-tool hooks (matches original HookRunner.run_pre_tool_use) ──
          const preHookResult = await runPreToolHooks(toolName, sessionId, toolArgs, workDir);

          if (!preHookResult.allowed) {
            // Hook denied the tool execution (exit code 2 = deny)
            toolOutput = preHookResult.message || `Tool '${toolName}' was denied by pre-tool hook`;
            isError = true;
            sendSSE(res, "permission_denied", {
              toolName,
              toolCallId: toolCall.id,
              reason: toolOutput,
              needsPrompt: false,
            });
          } else {
            // Execute the tool with the correct working directory
            const toolResult = await executeTool(toolName, toolArgs, sessionId, workDir);
            if (toolResult.isError && toolResult.output.includes("needs one-time approval")) {
              sendSSE(res, "permission_prompt", {
                toolName,
                toolCallId: toolCall.id,
                reason: toolResult.output,
              });
            }
            toolOutput = toolResult.output;
            isError = toolResult.isError || false;

            // Merge pre-hook feedback (matches original merge_hook_feedback)
            if (preHookResult.message) {
              toolOutput = mergeHookFeedback([preHookResult.message], toolOutput, false);
            }

            // ─── Post-tool hooks (matches original HookRunner.run_post_tool_use) ──
            const postHookResult = await runPostToolHooks(toolName, sessionId, toolResult, workDir);
            toolOutput = postHookResult.output;
            isError = postHookResult.isError || false;
          }
        } catch (toolExecError: any) {
          // ─── Bug #3 fix: Tool exception β†’ error result for LLM ──────
          // Original claw-code: tool errors become tool results, NOT loop breaks.
          // The LLM sees the error and can try a different approach.
          console.error(`[agent] Tool '${toolName}' threw exception:`, toolExecError.message);
          toolOutput = `Tool execution error: ${toolExecError.message}`;
          isError = true;
        }

        // No error classification or guidance injection.
        // The model receives raw error output and decides how to handle it.
        // This matches the original claw-code behavior.

        sendSSE(res, "tool_result", {
          toolCallId: toolCall.id,
          toolName,
          output: toolOutput,
          isError,
          durationMs: 0,
        });

        // ─── Special SSE events for interactive tools ────────────────

        // SendUserMessage / Brief: emit SSE for frontend display but DO NOT break the loop.
        // Original claw-code does NOT stop on SendUserMessage β€” the model can send
        // progress updates ("checking...", "found vulnerability...") AND continue working.
        // Breaking here was the #1 cause of the agent stopping mid-task.
        if ((toolName === "SendUserMessage" || toolName === "Brief" || toolName === "ask_user") && !isError) {
          sendSSE(res, "assistant_message", {
            message: toolArgs.message || toolArgs.question || "",
            attachments: toolArgs.attachments || [],
          });
        }

        // Plan/Todo tools: emit plan state updates
        if (["TodoWrite", "plan_create", "plan_update", "enter_plan_mode", "exit_plan_mode"].includes(toolName)) {
          const updatedPlan = getPlanMode(sessionId);
          sendSSE(res, "plan_update", {
            active: updatedPlan.active,
            steps: updatedPlan.steps,
          });
        }

        // Add tool result to conversation for the LLM to process
        const toolResultMsg: AgentMessage = {
          role: "tool",
          content: toolOutput,
          tool_call_id: toolCall.id,
          name: toolName,
        };
        conversationMessages.push(toolResultMsg);
        toolResultMessages.push(toolResultMsg);
      }

      // No consecutive error detection β€” the model handles errors naturally.
      // MAX_ITERATIONS (200) is the ultimate safety net.
      // SendUserMessage does NOT break the loop (matches original).

      // ─── Proactive auto-compact check ─────────────────────────────
      // Check if conversation is approaching context window limit and compact proactively
      const estimatedTokens = estimateConversationTokens(conversationMessages);
      // contextWindow already computed above (line 397)
      const contextUsagePercent = Math.round((estimatedTokens / contextWindow) * 100);

      // Emit context usage SSE for frontend tracking
      sendSSE(res, "context_usage", {
        estimatedTokens,
        contextWindow,
        usagePercent: contextUsagePercent,
        messageCount: conversationMessages.length,
      });

      // Proactive compaction at 80% context usage
      if (contextUsagePercent >= 80) {
        console.log(`[agent] Context at ${contextUsagePercent}% β€” proactive auto-compact`);
        sendSSE(res, "status", {
          status: "compacting",
          message: `Context at ${contextUsagePercent}% β€” auto-compacting to free space...`,
        });

        try {
          const session = agentMessagesToSession(conversationMessages);

          // LLM-based summarization for proactive compaction
          const llmFetchProactive = async (msgs: Array<{role: string; content: string}>) => {
            const summaryResp = await fetch(apiConfig.url, {
              method: "POST",
              headers: { "Content-Type": "application/json", Authorization: `Bearer ${apiConfig.key}` },
              body: JSON.stringify({
                model: apiConfig.model,
                messages: msgs,
                max_tokens: 2000,
                temperature: 0.3,
                stream: false,
              }),
            });
            if (!summaryResp.ok) throw new Error(`LLM summary failed: ${summaryResp.status}`);
            const json = await summaryResp.json();
            return json.choices?.[0]?.message?.content || "";
          };

          const compactResult = await compactSessionWithLLM(session, dynamicCompactionConfig, llmFetchProactive);
          if (compactResult.removedMessageCount > 0) {
            const compactedAgentMessages = sessionToAgentMessages(compactResult.compactedSession);
            conversationMessages.length = 0;
            // CRITICAL: Re-prepend original system prompt before compacted summary.
            conversationMessages.push({ role: "system", content: systemPrompt });
            conversationMessages.push(...compactedAgentMessages);

            // Inject current todo/plan state so the agent doesn't lose its plan after compaction
            const todoState = (() => {
              try {
                const executor = require("../tools/executor");
                const plan = executor.getPlanMode(sessionId);
                const todos = executor.todoLists?.get?.(sessionId) || [];
                let state = "";
                if (todos.length > 0) {
                  state += "\n\n[PRESERVED TODO LIST]\n" + todos.map((t: any, i: number) => {
                    const icon = t.status === "completed" ? "\u2713" : t.status === "in_progress" ? "\u25cf" : "\u25cb";
                    return `  ${icon} ${i + 1}. ${t.content} [${t.status}]`;
                  }).join("\n");
                }
                if (plan?.active && plan.steps?.length > 0) {
                  state += "\n\n[PRESERVED PLAN]\n" + plan.steps.map((s: any) => {
                    const icon = s.status === "done" ? "\u2713" : s.status === "in_progress" ? "\u25cf" : "\u25a1";
                    return `  ${icon} ${s.id}. ${s.text} [${s.status}]`;
                  }).join("\n");
                }
                return state;
              } catch { return ""; }
            })();

            if (todoState) {
              // Append todo state to the last user/system message so the model sees it
              const lastMsg = conversationMessages[conversationMessages.length - 1];
              if (lastMsg && typeof lastMsg.content === "string") {
                lastMsg.content += todoState;
              }
            }

            sendSSE(res, "auto_compact", {
              removedCount: compactResult.removedMessageCount,
              keptCount: conversationMessages.length,
              summary: compactResult.formattedSummary,
            });
            console.log(`[agent] Proactive compact: removed ${compactResult.removedMessageCount} messages`);
          }
        } catch (compactErr: any) {
          console.error(`[agent] Proactive compact failed (non-fatal):`, compactErr.message);
        }
      }

      // ─── Buddy events SSE ────────────────────────────────────────────
      // Emit buddy_event for each tool call so frontend can award XP
      for (const toolCall of result.toolCalls) {
        const tn = toolCall.function.name;
        sendSSE(res, "buddy_event", {
          type: "tool_call",
          toolName: tn,
          iteration: iterations,
        });
        // Special buddy events for file creation
        if (tn === "write_file" || tn === "create_file") {
          sendSSE(res, "buddy_event", {
            type: "file_created",
            toolName: tn,
            iteration: iterations,
          });
        }
      }

      // Continue the loop β€” LLM will see tool results and decide next action
      sendSSE(res, "status", {
        status: "thinking",
        message: `Processing tool results (iteration ${iterations}, context: ${contextUsagePercent}%)...`,
      });
    } catch (error: any) {
      // ─── Bug #2 fix: Distinguish fatal vs transient errors ────────
      // Only AbortError and unrecoverable errors should break the loop.
      // Stream/fetch errors are already handled above with retry logic.
      if (error.name === "AbortError" || signal?.aborted) {
        sendSSE(res, "status", { status: "cancelled", message: "Request cancelled" });
        break;
      }
      // For other errors, log and break (these are truly unexpected)
      console.error(`[agent] Unexpected error in agent loop:`, error.message, error.stack);
      sendSSE(res, "error", { message: error.message || "Unknown error" });
      break;
    }
  }

  if (iterations >= MAX_ITERATIONS) {
    sendSSE(res, "error", { message: `Maximum iterations (${MAX_ITERATIONS}) reached. Use /compact to reduce context and continue.` });
  }

  // ─── Buddy: session_completed event ─────────────────────────────────
  // Emit session_completed so Buddy can award XP for finishing a turn
  sendSSE(res, "buddy_event", {
    type: "session_completed",
    iterations,
    toolCallCount: toolResultMessages.length,
  });

  // Calculate cost using UsageTracker (matches original)
  const cumulativeUsage = usageTracker.cumulativeUsage();
  const modelPricing = pricingForModel(apiConfig.model) ?? defaultSonnetTierPricing();
  const costEstimate = estimateCostUsdWithPricing(cumulativeUsage, modelPricing);
  totalCost = totalCostUsd(costEstimate);

  // Emit usage summary lines (matches original summary_lines_for_model)
  const usageSummary = summaryLinesForModel(cumulativeUsage, "session", apiConfig.model);
  sendSSE(res, "usage", {
    promptTokens: totalPromptTokens,
    completionTokens: totalCompletionTokens,
    totalTokens: totalPromptTokens + totalCompletionTokens,
    cost: totalCost,
    cacheCreationTokens: cumulativeUsage.cache_creation_input_tokens,
    cacheReadTokens: cumulativeUsage.cache_read_input_tokens,
    usageSummary,
    turns: usageTracker.turns(),
    formattedCost: formatUsd(totalCost),
  });

  return {
    finalMessages: conversationMessages.filter((m) => m.role !== "system"),
    totalPromptTokens,
    totalCompletionTokens,
    totalCost,
    model: apiConfig.model,
  };
}

/**
 * Process a streaming response from the LLM API (OpenAI-compatible SSE format)
 */
async function processStream(
  response: globalThis.Response,
  res: Response,
  signal?: AbortSignal
): Promise<{
  content: string;
  toolCalls: Array<{
    id: string;
    type: "function";
    function: { name: string; arguments: string };
  }>;
  usage?: { prompt_tokens: number; completion_tokens: number; cache_creation_input_tokens?: number; cache_read_input_tokens?: number };
}> {
  const reader = response.body?.getReader();
  if (!reader) throw new Error("No response body");

  const decoder = new TextDecoder();
  let content = "";
  const toolCalls: Map<
    number,
    { id: string; type: "function"; function: { name: string; arguments: string } }
  > = new Map();
  let usage: { prompt_tokens: number; completion_tokens: number; cache_creation_input_tokens?: number; cache_read_input_tokens?: number } | undefined;
  let buffer = "";

  try {
    while (true) {
      if (signal?.aborted) break;

      const { done, value } = await reader.read();
      if (done) break;

      buffer += decoder.decode(value, { stream: true });

      // Process complete SSE lines
      const lines = buffer.split("\n");
      buffer = lines.pop() || "";

      for (const line of lines) {
        if (!line.startsWith("data: ")) continue;
        const data = line.slice(6).trim();
        if (data === "[DONE]") continue;

        try {
          const chunk = JSON.parse(data);
          const delta = chunk.choices?.[0]?.delta;

          // Detect API errors returned inside the SSE stream (e.g. DeepInfra "Operation not allowed")
          if (chunk.error) {
            const errMsg = chunk.error.message || chunk.error.type || JSON.stringify(chunk.error);
            console.error(`[agent] API error in stream: ${errMsg}`);
            throw new Error(`API error in stream: ${errMsg}`);
          }

          if (!delta) {
            if (chunk.usage) {
              usage = {
                prompt_tokens: chunk.usage.prompt_tokens || 0,
                completion_tokens: chunk.usage.completion_tokens || 0,
                cache_creation_input_tokens: chunk.usage.cache_creation_input_tokens || 0,
                cache_read_input_tokens: chunk.usage.cache_read_input_tokens || 0,
              };
            }
            continue;
          }

          // Reasoning/thinking content (Qwen3 Thinking, DeepSeek-R1)
          // These models return reasoning in delta.reasoning_content before the actual response
          if (delta.reasoning_content) {
            sendSSE(res, "thinking_delta", { text: delta.reasoning_content });
          }

          // Text content streaming
          if (delta.content) {
            content += delta.content;
            sendSSE(res, "text_delta", { text: delta.content });
          }

          // Tool call streaming
          if (delta.tool_calls) {
            for (const tc of delta.tool_calls) {
              const idx = tc.index ?? 0;
              if (!toolCalls.has(idx)) {
                toolCalls.set(idx, {
                  id: tc.id || `call_${idx}_${Date.now()}`,
                  type: "function",
                  function: { name: tc.function?.name || "", arguments: "" },
                });
              }
              const existing = toolCalls.get(idx)!;
              if (tc.id) existing.id = tc.id;
              if (tc.function?.name) existing.function.name = tc.function.name;
              if (tc.function?.arguments) {
                existing.function.arguments += tc.function.arguments;
                sendSSE(res, "tool_call_delta", {
                  id: existing.id,
                  name: existing.function.name,
                  arguments: tc.function.arguments,
                });
              }
            }
          }

          // Usage info
          if (chunk.usage) {
            usage = {
              prompt_tokens: chunk.usage.prompt_tokens || 0,
              completion_tokens: chunk.usage.completion_tokens || 0,
              cache_creation_input_tokens: chunk.usage.cache_creation_input_tokens || 0,
              cache_read_input_tokens: chunk.usage.cache_read_input_tokens || 0,
            };
          }
        } catch (parseErr: any) {
          // Re-throw API errors (these are NOT malformed JSON β€” they're real errors)
          if (parseErr?.message?.startsWith('API error in stream:')) {
            throw parseErr;
          }
          // Skip genuinely malformed JSON chunks (partial SSE data, etc.)
        }
      }
    }
  } finally {
    reader.releaseLock();
  }

  // Process remaining buffer (last line without trailing \n)
  if (buffer.trim() && buffer.startsWith("data: ")) {
    const data = buffer.slice(6).trim();
    if (data !== "[DONE]") {
      try {
        const chunk = JSON.parse(data);
        const delta = chunk.choices?.[0]?.delta;
        if (delta?.content) {
          content += delta.content;
          sendSSE(res, "text_delta", { text: delta.content });
        }
        if (delta?.tool_calls) {
          for (const tc of delta.tool_calls) {
            const idx = tc.index ?? 0;
            if (!toolCalls.has(idx)) {
              toolCalls.set(idx, {
                id: tc.id || `call_${idx}_${Date.now()}`,
                type: "function",
                function: { name: tc.function?.name || "", arguments: "" },
              });
            }
            const existing = toolCalls.get(idx)!;
            if (tc.id) existing.id = tc.id;
            if (tc.function?.name) existing.function.name = tc.function.name;
            if (tc.function?.arguments) existing.function.arguments += tc.function.arguments;
          }
        }
        if (chunk.usage) {
          usage = {
            prompt_tokens: chunk.usage.prompt_tokens || 0,
            completion_tokens: chunk.usage.completion_tokens || 0,
            cache_creation_input_tokens: chunk.usage.cache_creation_input_tokens || 0,
            cache_read_input_tokens: chunk.usage.cache_read_input_tokens || 0,
          };
        }
        // Check finish_reason for truncation
        const finishReason = chunk.choices?.[0]?.finish_reason;
        if (finishReason === "length") {
          console.warn("[agent] Response truncated (finish_reason=length) β€” tool call args may be incomplete");
        }
      } catch { /* skip malformed */ }
    }
  }

  // Original claw-code retries on empty response instead of throwing.
  if (content.length === 0 && toolCalls.size === 0) {
    console.warn("[agent] LLM returned empty response β€” will be retried by agent loop");
  }

  return {
    content,
    toolCalls: Array.from(toolCalls.values()),
    usage,
  };
}

/**
 * Estimate cost based on model and token counts
 */
function estimateCost(model: string, promptTokens: number, completionTokens: number): number {
  // Pricing per 1M tokens β€” aligned with original claw-code model registry
  const pricing: Record<string, { input: number; output: number }> = {
    // Claw API / Anthropic
    "claude-opus-4-6": { input: 15.00, output: 75.00 },
    "claude-sonnet-4-6": { input: 3.00, output: 15.00 },
    "claude-haiku-4-5-20251213": { input: 0.80, output: 4.00 },
    // xAI Grok
    "grok-3": { input: 3.00, output: 15.00 },
    "grok-3-mini": { input: 0.30, output: 0.50 },
    "grok-2": { input: 2.00, output: 10.00 },
    // OpenAI
    "gpt-5.4": { input: 2.50, output: 15.00 },
    "gpt-5.4-mini": { input: 0.40, output: 1.60 },
    "gpt-5.3-codex": { input: 2.50, output: 10.00 },
    "gpt-4.1": { input: 2.00, output: 8.00 },
    "gpt-4.1-mini": { input: 0.40, output: 1.60 },
    "o3": { input: 10.00, output: 40.00 },
    "o4-mini": { input: 1.10, output: 4.40 },
    // HuggingFace Inference API (free tier = $0, Pro tier = included in subscription)
    "XiaomiMiMo/MiMo-V2-Flash": { input: 0.00, output: 0.00 },
    "Qwen/Qwen3-Coder-Next": { input: 0.00, output: 0.00 },
    "Qwen/Qwen3-8B": { input: 0.00, output: 0.00 },
    "Qwen/Qwen3-Coder-30B-A3B-Instruct": { input: 0.00, output: 0.00 },
    "meta-llama/Llama-3.3-70B-Instruct": { input: 0.00, output: 0.00 },
    "deepseek-ai/DeepSeek-V3.2": { input: 0.00, output: 0.00 },
    "deepseek-ai/DeepSeek-R1": { input: 0.00, output: 0.00 },
    // OpenRouter variants
    "anthropic/claude-opus-4-6": { input: 15.00, output: 75.00 },
    "anthropic/claude-sonnet-4-6": { input: 3.00, output: 15.00 },
    "google/gemini-2.5-pro": { input: 1.25, output: 10.00 },
    "google/gemini-2.5-flash": { input: 0.15, output: 0.60 },
  };

  const rates = pricing[model] || { input: 1.00, output: 3.00 };
  return (promptTokens * rates.input + completionTokens * rates.output) / 1_000_000;
}