opencrabs 0.3.9

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
//! Qwen Code CLI Provider — direct subprocess integration
//!
//! Spawns the `qwen` CLI binary as a text completion backend and reads
//! its NDJSON stream output, converting it to standard `StreamEvent`s.
//! OpenCrabs handles all tools, memory, and context locally.
//!
//! Binary: `qwen` (npm: @qwen-code/qwen-code)
//! Free tier: 1,000 req/day via Qwen OAuth (hard-coded, no config needed).
//! Also supports OpenAI/Anthropic/Gemini-compatible APIs via settings.json.

use super::error::{ProviderError, Result};
use super::r#trait::{Provider, ProviderStream};
use super::types::*;
use async_trait::async_trait;
use futures::stream::StreamExt;
use serde::Deserialize;
use std::process::Stdio;
use tokio::io::AsyncBufReadExt;
use tokio::io::AsyncWriteExt;

/// Ensure `~/.qwen/settings.json` has `general.gitCoAuthor: false`.
///
/// Qwen CLI only reads this setting from disk — no CLI flag or env var.
/// If the file was wiped or never had the key, we patch it in-place
/// before every spawn so co-author trailers never sneak through.
fn ensure_no_git_coauthor() {
    let Some(home) = dirs::home_dir() else {
        return;
    };
    let path = home.join(".qwen").join("settings.json");
    let mut root: serde_json::Value = match std::fs::read_to_string(&path) {
        Ok(s) => match serde_json::from_str(&s) {
            Ok(v) => v,
            Err(_) => return, // corrupt — don't touch
        },
        Err(_) => return, // missing — qwen CLI will create it
    };

    let general = root.as_object_mut().and_then(|o| {
        o.entry("general")
            .or_insert_with(|| serde_json::json!({}))
            .as_object_mut()
    });

    if let Some(g) = general {
        if g.get("gitCoAuthor") == Some(&serde_json::Value::Bool(false)) {
            return; // already correct
        }
        g.insert("gitCoAuthor".into(), serde_json::Value::Bool(false));
    } else {
        return;
    }

    if let Ok(pretty) = serde_json::to_string_pretty(&root) {
        let _ = std::fs::write(&path, pretty);
        tracing::info!("patched ~/.qwen/settings.json: general.gitCoAuthor = false");
    }
}

/// Qwen Code CLI provider — talks directly to the `qwen` binary.
#[derive(Clone)]
pub struct QwenCodeCliProvider {
    qwen_path: String,
    default_model: String,
}

impl QwenCodeCliProvider {
    /// Create a new provider, auto-detecting the qwen binary.
    pub fn new() -> Result<Self> {
        let path = resolve_qwen_path()?;
        Ok(Self {
            qwen_path: path,
            default_model: "qwen3-coder-plus".to_string(),
        })
    }

    /// Override the default model.
    pub fn with_default_model(mut self, model: String) -> Self {
        self.default_model = model;
        self
    }

    /// Build a plain-text prompt from LLMRequest messages.
    fn build_prompt(request: &LLMRequest) -> String {
        let mut parts = Vec::new();

        if let Some(ref system) = request.system
            && !system.is_empty()
        {
            parts.push(system.clone());
        }

        for msg in &request.messages {
            let role = match msg.role {
                Role::User => "Human",
                Role::Assistant => "Assistant",
                Role::System => "System",
            };
            let content: String = msg
                .content
                .iter()
                .filter_map(|b| match b {
                    ContentBlock::Text { text } => Some(text.clone()),
                    ContentBlock::ToolResult {
                        tool_use_id,
                        content,
                        ..
                    } => Some(format!("[tool_result for {}]: {}", tool_use_id, content)),
                    ContentBlock::ToolUse { id, name, input } => {
                        Some(format!("[tool_use {} ({}): {}]", name, id, input))
                    }
                    ContentBlock::Thinking { thinking, .. } => {
                        if thinking.is_empty() {
                            None
                        } else {
                            Some(format!("<thinking>{}</thinking>", thinking))
                        }
                    }
                    ContentBlock::Image { source } => {
                        Some(match source {
                            ImageSource::Base64 { media_type, data } => {
                                let ext = match media_type.as_str() {
                                    "image/png" => "png",
                                    "image/jpeg" => "jpeg",
                                    "image/gif" => "gif",
                                    "image/webp" => "webp",
                                    _ => "png",
                                };
                                let tmp = std::env::temp_dir().join(format!(
                                    "opencrabs_qwen_img_{}.{}",
                                    uuid::Uuid::new_v4(),
                                    ext
                                ));
                                use base64::Engine;
                                if let Ok(bytes) = base64::engine::general_purpose::STANDARD.decode(data)
                                    && std::fs::write(&tmp, &bytes).is_ok()
                                {
                                    format!(
                                        "[User attached an image at {}. Use the analyze_image tool to view it.]",
                                        tmp.display()
                                    )
                                } else {
                                    "[User attached an image but it could not be decoded.]".to_string()
                                }
                            }
                            ImageSource::Url { url } => {
                                format!(
                                    "[User attached an image: {}. Use the analyze_image tool to view it.]",
                                    url
                                )
                            }
                        })
                    }
                })
                .collect::<Vec<_>>()
                .join("\n");

            if content.trim().is_empty() {
                continue;
            }
            parts.push(format!("{}: {}", role, content));
        }

        parts.join("\n\n")
    }
}

/// Resolve the qwen CLI binary path.
fn resolve_qwen_path() -> Result<String> {
    if let Ok(path) = std::env::var("QWEN_CODE_PATH") {
        if std::path::Path::new(&path).exists() {
            return Ok(path);
        }
        return Err(ProviderError::Internal(format!(
            "QWEN_CODE_PATH set but not found: {}",
            path
        )));
    }

    // Try common install locations (npm global, brew, home install script)
    for candidate in &["/opt/homebrew/bin/qwen", "/usr/local/bin/qwen"] {
        if std::path::Path::new(candidate).exists() {
            return Ok(candidate.to_string());
        }
    }

    // Try PATH lookup (cross-platform: `which` on Unix, `where.exe` on Windows)
    if let Some(path) = super::which_binary("qwen") {
        return Ok(path);
    }
    if let Some(path) = super::which_binary("qwen-code") {
        return Ok(path);
    }

    Err(ProviderError::Internal(
        "qwen CLI not found — install with `npm install -g @qwen-code/qwen-code` or set QWEN_CODE_PATH".to_string(),
    ))
}

// ── CLI NDJSON types — Qwen uses the same Gemini/Anthropic protocol ──

#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum CliMessage {
    System {
        #[allow(dead_code)]
        model: Option<String>,
    },
    Assistant {
        message: CliAssistantMessage,
    },
    StreamEvent {
        event: serde_json::Value,
    },
    User {
        #[allow(dead_code)]
        #[serde(default)]
        message: serde_json::Value,
    },
    RateLimitEvent {},
    Result {
        stop_reason: Option<String>,
        usage: Option<CliUsage>,
        #[serde(default)]
        is_error: bool,
        result: Option<String>,
    },
}

#[derive(Debug, Deserialize)]
struct CliAssistantMessage {
    pub id: Option<String>,
    pub model: Option<String>,
    pub usage: Option<CliUsage>,
    #[serde(default)]
    pub content: Vec<CliContentBlock>,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum CliContentBlock {
    Text {
        text: String,
    },
    ToolUse {
        id: String,
        name: String,
        input: serde_json::Value,
    },
    Thinking {
        thinking: String,
    },
    #[serde(other)]
    Unknown,
}

#[derive(Debug, Clone, Deserialize)]
struct CliUsage {
    #[serde(default)]
    pub input_tokens: u32,
    #[serde(default)]
    pub output_tokens: u32,
    #[serde(default)]
    pub cache_creation_input_tokens: u32,
    #[serde(default)]
    pub cache_read_input_tokens: u32,
}

impl CliUsage {
    fn total_input(&self) -> u32 {
        self.input_tokens
    }
}

#[async_trait]
impl Provider for QwenCodeCliProvider {
    async fn complete(&self, request: LLMRequest) -> Result<LLMResponse> {
        let mut stream = self.stream(request).await?;

        let mut id = String::new();
        let mut model = String::new();
        let mut content = Vec::new();
        let mut stop_reason = None;
        let mut usage = TokenUsage::default();
        let mut text_buf = String::new();

        while let Some(event) = stream.next().await {
            match event? {
                StreamEvent::MessageStart { message } => {
                    id = message.id;
                    model = message.model;
                    usage = message.usage;
                }
                StreamEvent::ContentBlockDelta {
                    delta: ContentDelta::TextDelta { text },
                    ..
                } => {
                    text_buf.push_str(&text);
                }
                StreamEvent::MessageDelta { delta: d, usage: u } => {
                    stop_reason = d.stop_reason;
                    usage.output_tokens = u.output_tokens;
                    if u.cache_creation_tokens > 0 {
                        usage.cache_creation_tokens = u.cache_creation_tokens;
                    }
                    if u.cache_read_tokens > 0 {
                        usage.cache_read_tokens = u.cache_read_tokens;
                    }
                }
                StreamEvent::MessageStop => break,
                _ => {}
            }
        }

        if !text_buf.is_empty() {
            content.push(ContentBlock::Text { text: text_buf });
        }

        Ok(LLMResponse {
            id,
            model,
            content,
            stop_reason,
            usage,
        })
    }

    async fn stream(&self, request: LLMRequest) -> Result<ProviderStream> {
        ensure_no_git_coauthor();

        let prompt = Self::build_prompt(&request);
        let model = request.model.clone();

        let cwd = request
            .working_directory
            .as_deref()
            .map(std::path::PathBuf::from)
            .filter(|p| p.is_dir())
            .unwrap_or_else(|| dirs::home_dir().unwrap_or_else(|| std::path::PathBuf::from("/")));

        tracing::info!(
            "Spawning qwen CLI: model={}, prompt_len={}, cwd={}",
            model,
            prompt.len(),
            cwd.display()
        );

        let session_id_str = uuid::Uuid::new_v4().to_string();

        let mut child = tokio::process::Command::new(&self.qwen_path)
            .env_remove("QWEN_CODE")
            .arg("-p")
            .arg("--output-format")
            .arg("stream-json")
            .arg("--include-partial-messages")
            .arg("--session-id")
            .arg(&session_id_str)
            .arg("--yolo")
            // Qwen Code CLI has no `--settings` flag and no env var to
            // disable the `Co-authored-by: Qwen-Coder` git-commit trailer
            // (the `general.gitCoAuthor` setting is only configurable via
            // ~/.qwen/settings.json). Force it off at the system-prompt
            // level so every spawn — including brand-new installs — never
            // ships an attribution trailer we didn't authorize.
            .arg("--append-system-prompt")
            .arg(
                "HARD RULE: When creating git commits, NEVER append any \
                 `Co-authored-by:` trailer (Qwen-Coder, Qwen, or otherwise) \
                 and NEVER add `Generated with Qwen Code` footers. Commit \
                 messages must contain only the user-authored content.",
            )
            .arg("--model")
            .arg(&model)
            .current_dir(&cwd)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .map_err(|e| ProviderError::Internal(format!("failed to spawn qwen CLI: {}", e)))?;

        let mut stdin = child
            .stdin
            .take()
            .ok_or_else(|| ProviderError::Internal("failed to capture stdin".to_string()))?;
        let prompt_bytes = prompt.into_bytes();
        tokio::spawn(async move {
            let _ = stdin.write_all(&prompt_bytes).await;
            let _ = stdin.shutdown().await;
        });

        let stdout = child
            .stdout
            .take()
            .ok_or_else(|| ProviderError::Internal("failed to capture stdout".to_string()))?;

        let stderr = child
            .stderr
            .take()
            .ok_or_else(|| ProviderError::Internal("failed to capture stderr".to_string()))?;

        tokio::spawn(async move {
            let reader = tokio::io::BufReader::new(stderr);
            let mut lines = reader.lines();
            while let Ok(Some(line)) = lines.next_line().await {
                let line = line.trim().to_string();
                if !line.is_empty() {
                    tracing::warn!("qwen CLI stderr: {}", line);
                }
            }
        });

        let (tx, rx) = tokio::sync::mpsc::channel::<Result<StreamEvent>>(64);

        // Spawn a dedicated serial writer task for streaming DB persistence.
        // qwen-cli runs its entire yolo agentic loop (100s of tool calls) in a
        // single subprocess invocation = one tool_loop iteration. The normal
        // CLI persistence path in tool_loop only writes once at iteration end,
        // so a mid-run cancel (double-Esc) loses everything accumulated in
        // memory. We bypass that here by streaming text + tool markers straight
        // to the assistant message row as the qwen subprocess emits them,
        // matching the per-tool real-time persistence non-CLI providers get
        // via tool_loop.rs:2210.
        let persist_session = request.session_id;
        let (persist_tx, mut persist_rx) = tokio::sync::mpsc::unbounded_channel::<String>();
        tokio::spawn(async move {
            while let Some(chunk) = persist_rx.recv().await {
                if let Some(sid) = persist_session {
                    persist_qwen_chunk(sid, &chunk).await;
                }
            }
        });

        tokio::spawn(async move {
            let reader = tokio::io::BufReader::new(stdout);
            let mut lines = reader.lines();

            let mut started = false;
            let mut streaming_via_events = false;
            let mut completed_blocks: usize = 0;
            let mut current_block_started = false;
            let mut current_block_chars: usize = 0;
            let mut input_tokens: u32 = 0;
            let mut output_tokens: u32 = 0;
            let mut cache_creation_tokens_last: u32 = 0;
            let mut cache_read_tokens_last: u32 = 0;
            let mut cache_creation_tokens_billing: u32 = 0;
            let mut cache_read_tokens_billing: u32 = 0;
            let mut result_received = false;
            let mut line_count = 0u32;
            let mut block_index_offset: usize = 0;
            let mut max_block_index_this_round: usize = 0;
            // Tracks whether any tool_use block was seen during the entire stream,
            // logged at EOF for diagnostics. Qwen CLI never emits a `result`
            // envelope, so MessageStop is synthesized on EOF.
            let mut saw_tool_use_in_message = false;

            // Buffer for streaming text deltas — flushed to DB whenever a tool
            // block arrives, on subprocess EOF, or when the buffer grows past
            // FLUSH_SIZE. Text is written as plain content so it survives
            // cancel / session reload exactly like non-CLI providers.
            let mut text_persist_buf = String::new();
            const FLUSH_SIZE: usize = 256;
            // Deduplicate tool markers per (block_index, offset) so we don't
            // double-write when qwen-cli re-emits the same tool_use on every
            // streaming update of a block.
            let mut persisted_tool_blocks: std::collections::HashSet<usize> =
                std::collections::HashSet::new();

            loop {
                let line_result = tokio::select! {
                    biased;
                    _ = tx.closed() => {
                        tracing::info!("Qwen CLI stream cancelled — killing subprocess");
                        let _ = child.kill().await;
                        break;
                    }
                    result = lines.next_line() => result,
                };
                let line = match line_result {
                    Ok(Some(line)) => line,
                    Ok(None) => {
                        tracing::info!(
                            "Qwen CLI stdout EOF after {} lines (started={})",
                            line_count,
                            started
                        );
                        break;
                    }
                    Err(e) => {
                        tracing::error!(
                            "Qwen CLI stdout read error after {} lines: {}",
                            line_count,
                            e
                        );
                        break;
                    }
                };
                line_count += 1;
                let line = line.trim().to_string();
                if line.is_empty() {
                    continue;
                }

                tracing::debug!(
                    "Qwen CLI stdout raw: {}",
                    &line[..line.floor_char_boundary(2000)]
                );

                let msg: CliMessage = match serde_json::from_str(&line) {
                    Ok(m) => m,
                    Err(e) => {
                        tracing::warn!(
                            "Skipping unparseable Qwen CLI line: {} — {}",
                            e,
                            &line[..line.floor_char_boundary(200)]
                        );
                        continue;
                    }
                };

                match msg {
                    CliMessage::System { .. } => {
                        tracing::debug!("Qwen CLI → system");
                    }

                    CliMessage::StreamEvent { event } => {
                        let event_type = event.get("type").and_then(|t| t.as_str()).unwrap_or("");
                        streaming_via_events = true;

                        match event_type {
                            "message_start" => {
                                if !started {
                                    started = true;
                                    // Qwen's `message_start` often ships WITHOUT a
                                    // `usage` field, which makes the strict
                                    // `StreamEvent::MessageStart` deserializer fail.
                                    // Build the event manually so missing/loose
                                    // fields can't break the stream header — without
                                    // it, the agent loop never sees a stream begin
                                    // and treats the whole turn as dropped.
                                    if let Some(msg) = event.get("message")
                                        && let Some(u) = msg.get("usage")
                                        && let Ok(cli_u) =
                                            serde_json::from_value::<CliUsage>(u.clone())
                                    {
                                        input_tokens = cli_u.total_input();
                                    }
                                    let id = event
                                        .get("message")
                                        .and_then(|m| m.get("id"))
                                        .and_then(|v| v.as_str())
                                        .map(str::to_string)
                                        .unwrap_or_else(|| {
                                            format!("msg_{}", uuid::Uuid::new_v4().simple())
                                        });
                                    let model_name = event
                                        .get("message")
                                        .and_then(|m| m.get("model"))
                                        .and_then(|v| v.as_str())
                                        .map(str::to_string)
                                        .unwrap_or_else(|| request.model.clone());
                                    let se = StreamEvent::MessageStart {
                                        message: StreamMessage {
                                            id,
                                            model: model_name,
                                            role: Role::Assistant,
                                            usage: TokenUsage {
                                                input_tokens,
                                                output_tokens: 0,
                                                ..Default::default()
                                            },
                                        },
                                    };
                                    if tx.send(Ok(se)).await.is_err() {
                                        break;
                                    }
                                }
                            }
                            "message_delta" => {
                                let is_tool_round = event
                                    .get("delta")
                                    .and_then(|d| d.get("stop_reason"))
                                    .and_then(|r| r.as_str())
                                    == Some("tool_use");
                                if is_tool_round {
                                    block_index_offset += max_block_index_this_round;
                                    max_block_index_this_round = 0;
                                }
                                if let Some(u) = event.get("usage") {
                                    let round_output = u
                                        .get("output_tokens")
                                        .and_then(|v| v.as_u64())
                                        .unwrap_or(0)
                                        as u32;
                                    output_tokens += round_output;
                                    if let Ok(round_usage) =
                                        serde_json::from_value::<CliUsage>(u.clone())
                                    {
                                        let round_input = round_usage.total_input();
                                        if round_input > input_tokens {
                                            input_tokens = round_input;
                                        }
                                        cache_creation_tokens_last =
                                            round_usage.cache_creation_input_tokens;
                                        cache_read_tokens_last =
                                            round_usage.cache_read_input_tokens;
                                        cache_creation_tokens_billing +=
                                            round_usage.cache_creation_input_tokens;
                                        cache_read_tokens_billing +=
                                            round_usage.cache_read_input_tokens;
                                    }
                                }
                                if tx.send(Ok(StreamEvent::Ping)).await.is_err() {
                                    break;
                                }
                            }
                            "message_stop" => {
                                // NOT the end of conversation — qwen CLI in --yolo
                                // mode runs its own tools and continues with more
                                // assistant messages. Each `message_stop` just
                                // marks the end of one assistant turn. The real
                                // termination is process EOF (handled below).
                                tracing::debug!(
                                    "Qwen CLI → message_stop (saw_tool_use={})",
                                    saw_tool_use_in_message
                                );
                                if tx.send(Ok(StreamEvent::Ping)).await.is_err() {
                                    break;
                                }
                            }
                            "content_block_start"
                                if event
                                    .get("content_block")
                                    .and_then(|b| b.get("type"))
                                    .and_then(|t| t.as_str())
                                    == Some("tool_use") =>
                            {
                                // Track that we saw a tool_use, but DO forward
                                // the block downstream so it gets persisted to
                                // the DB and rendered in the TUI. The agent
                                // loop short-circuits actual execution via
                                // `cli_handles_tools()`.
                                saw_tool_use_in_message = true;
                                match serde_json::from_value::<StreamEvent>(event.clone()) {
                                    Ok(se) => {
                                        if let StreamEvent::ContentBlockStart { index, .. } = &se {
                                            max_block_index_this_round =
                                                max_block_index_this_round.max(index + 1);
                                        }
                                        let se = offset_block_index(se, block_index_offset);
                                        let se = normalize_stream_event(se);
                                        // ── Real-time DB persistence ──
                                        // Flush any accumulated text first so
                                        // the on-disk order matches the stream
                                        // (text → tools → text), then write a
                                        // tools-v2 marker for this tool call.
                                        if let StreamEvent::ContentBlockStart {
                                            index: abs_index,
                                            content_block: ContentBlock::ToolUse { name, input, .. },
                                        } = &se
                                            && persisted_tool_blocks.insert(*abs_index)
                                        {
                                            if !text_persist_buf.is_empty() {
                                                let _ = persist_tx.send(format!(
                                                    "{}\n\n",
                                                    std::mem::take(&mut text_persist_buf)
                                                ));
                                            }
                                            let _ =
                                                persist_tx.send(format_tool_marker(name, input));
                                        }
                                        if tx.send(Ok(se)).await.is_err() {
                                            break;
                                        }
                                    }
                                    Err(e) => {
                                        tracing::debug!("tool_use content_block_start: {}", e);
                                    }
                                }
                            }
                            _ => match serde_json::from_value::<StreamEvent>(event.clone()) {
                                Ok(se) => {
                                    match &se {
                                        StreamEvent::ContentBlockStart { index, .. }
                                        | StreamEvent::ContentBlockDelta { index, .. }
                                        | StreamEvent::ContentBlockStop { index } => {
                                            max_block_index_this_round =
                                                max_block_index_this_round.max(index + 1);
                                        }
                                        _ => {}
                                    }
                                    let se = offset_block_index(se, block_index_offset);
                                    let se = normalize_stream_event(se);
                                    // Capture streaming text deltas for direct
                                    // DB persistence — flush on tool boundary,
                                    // buffer overflow, or subprocess EOF.
                                    if let StreamEvent::ContentBlockDelta {
                                        delta: ContentDelta::TextDelta { text },
                                        ..
                                    } = &se
                                    {
                                        text_persist_buf.push_str(text);
                                        if text_persist_buf.len() >= FLUSH_SIZE {
                                            let _ = persist_tx
                                                .send(std::mem::take(&mut text_persist_buf));
                                        }
                                    }
                                    if tx.send(Ok(se)).await.is_err() {
                                        break;
                                    }
                                }
                                Err(e) => {
                                    tracing::debug!(
                                        "Skipping stream event '{}': {}",
                                        event_type,
                                        e
                                    );
                                }
                            },
                        }
                    }

                    CliMessage::Assistant { message } => {
                        if streaming_via_events {
                            if let Some(u) = &message.usage {
                                // Each `assistant` envelope reports the usage
                                // for ONE agentic round (one model call). Track
                                // per-round LAST values for context-window
                                // display, and accumulate billing separately.
                                // Without this, the per-round counters stay 0
                                // for qwen (it never emits `message_delta`),
                                // and the calibration falls back to the
                                // cumulative `result.usage` which inflates
                                // ctx % by num_turns (e.g. 2700% on 19 turns).
                                output_tokens = u.output_tokens;
                                input_tokens = u.input_tokens;
                                cache_creation_tokens_last = u.cache_creation_input_tokens;
                                cache_read_tokens_last = u.cache_read_input_tokens;
                                cache_creation_tokens_billing += u.cache_creation_input_tokens;
                                cache_read_tokens_billing += u.cache_read_input_tokens;
                                tracing::info!(
                                    "Qwen per-round usage: in={}, out={}, cc={}, cr={}",
                                    u.input_tokens,
                                    u.output_tokens,
                                    u.cache_creation_input_tokens,
                                    u.cache_read_input_tokens,
                                );
                            }
                            continue;
                        }

                        if !started {
                            started = true;
                            let msg_id = message.id.unwrap_or_else(|| {
                                format!("msg_{}", uuid::Uuid::new_v4().simple())
                            });
                            let msg_model = message
                                .model
                                .clone()
                                .unwrap_or_else(|| request.model.clone());
                            let (input_tokens, cc, cr) = message
                                .usage
                                .as_ref()
                                .map(|u| {
                                    (
                                        u.total_input(),
                                        u.cache_creation_input_tokens,
                                        u.cache_read_input_tokens,
                                    )
                                })
                                .unwrap_or((0, 0, 0));

                            let _ = tx
                                .send(Ok(StreamEvent::MessageStart {
                                    message: StreamMessage {
                                        id: msg_id,
                                        model: msg_model,
                                        role: Role::Assistant,
                                        usage: TokenUsage {
                                            input_tokens,
                                            output_tokens: 0,
                                            cache_creation_tokens: cc,
                                            cache_read_tokens: cr,
                                            ..Default::default()
                                        },
                                    },
                                }))
                                .await;
                        }

                        let num_blocks = message.content.len();
                        for (i, block) in message.content.iter().enumerate() {
                            let is_last = i == num_blocks - 1;

                            if i < completed_blocks {
                                continue;
                            }

                            if i > completed_blocks {
                                if current_block_started {
                                    let _ = tx
                                        .send(Ok(StreamEvent::ContentBlockStop {
                                            index: completed_blocks,
                                        }))
                                        .await;
                                    completed_blocks += 1;
                                    current_block_chars = 0;
                                    current_block_started = false;
                                }

                                while completed_blocks < i {
                                    emit_full_block(
                                        &tx,
                                        &message.content[completed_blocks],
                                        completed_blocks,
                                    )
                                    .await;
                                    completed_blocks += 1;
                                }
                            }

                            let full_text = cli_block_text(block);

                            if !current_block_started {
                                let empty = cli_empty_block(block);
                                let _ = tx
                                    .send(Ok(StreamEvent::ContentBlockStart {
                                        index: i,
                                        content_block: empty,
                                    }))
                                    .await;
                                current_block_started = true;
                                current_block_chars = 0;
                            }

                            if full_text.len() > current_block_chars {
                                let new_text = &full_text[current_block_chars..];
                                if !new_text.is_empty() {
                                    let delta = cli_block_delta(block, new_text);
                                    let _ = tx
                                        .send(Ok(StreamEvent::ContentBlockDelta {
                                            index: i,
                                            delta,
                                        }))
                                        .await;
                                    current_block_chars = full_text.len();
                                }
                            }

                            if !is_last {
                                let _ = tx
                                    .send(Ok(StreamEvent::ContentBlockStop { index: i }))
                                    .await;
                                completed_blocks += 1;
                                current_block_chars = 0;
                                current_block_started = false;
                            }
                        }

                        if let Some(u) = &message.usage {
                            output_tokens = u.output_tokens;
                        }
                    }

                    CliMessage::Result {
                        stop_reason,
                        usage,
                        is_error,
                        result,
                    } => {
                        if is_error {
                            let error_text =
                                result.unwrap_or_else(|| "CLI returned an error".to_string());
                            tracing::error!("Qwen CLI result is_error=true: {}", error_text);

                            let error_lower = error_text.to_lowercase();

                            if error_lower.contains("prompt is too long")
                                || error_lower.contains("too many tokens")
                                || error_lower.contains("context length")
                            {
                                let _ = tx.send(Err(ProviderError::ContextLengthExceeded(0))).await;
                                break;
                            }

                            if error_lower.contains("rate limit")
                                || error_lower.contains("hit your limit")
                                || error_lower.contains("overloaded")
                                || error_lower.contains("too many requests")
                                || error_lower.contains("capacity")
                                || error_lower.contains("429")
                            {
                                tracing::warn!(
                                    "Qwen CLI rate/account limit — returning RateLimitExceeded"
                                );
                                let _ = tx
                                    .send(Err(ProviderError::RateLimitExceeded(error_text)))
                                    .await;
                                break;
                            }

                            if !started {
                                started = true;
                                let _ = tx
                                    .send(Ok(StreamEvent::MessageStart {
                                        message: StreamMessage {
                                            id: format!("msg_{}", uuid::Uuid::new_v4().simple()),
                                            model: request.model.clone(),
                                            role: Role::Assistant,
                                            usage: TokenUsage {
                                                input_tokens: 0,
                                                output_tokens: 0,
                                                ..Default::default()
                                            },
                                        },
                                    }))
                                    .await;
                            }

                            if current_block_started {
                                let _ = tx
                                    .send(Ok(StreamEvent::ContentBlockStop {
                                        index: completed_blocks,
                                    }))
                                    .await;
                                completed_blocks += 1;
                            }

                            let error_idx = completed_blocks + block_index_offset;
                            let _ = tx
                                .send(Ok(StreamEvent::ContentBlockStart {
                                    index: error_idx,
                                    content_block: ContentBlock::Text {
                                        text: String::new(),
                                    },
                                }))
                                .await;
                            let _ = tx
                                .send(Ok(StreamEvent::ContentBlockDelta {
                                    index: error_idx,
                                    delta: ContentDelta::TextDelta {
                                        text: format!("\n\n⚠️ CLI error: {}", error_text),
                                    },
                                }))
                                .await;
                            let _ = tx
                                .send(Ok(StreamEvent::ContentBlockStop { index: error_idx }))
                                .await;
                        } else {
                            if current_block_started {
                                let _ = tx
                                    .send(Ok(StreamEvent::ContentBlockStop {
                                        index: completed_blocks,
                                    }))
                                    .await;
                            }
                        }

                        // Qwen CLI's `result` envelope often omits `stop_reason`
                        // entirely on success. Default to EndTurn so the agent
                        // loop doesn't treat the stream as dropped and retry,
                        // which manifested as the same response looping 5–6 times.
                        let reason = Some(
                            stop_reason
                                .map(|r| match r.as_str() {
                                    "end_turn" => StopReason::EndTurn,
                                    "tool_use" => StopReason::ToolUse,
                                    "max_tokens" => StopReason::MaxTokens,
                                    _ => StopReason::EndTurn,
                                })
                                .unwrap_or(StopReason::EndTurn),
                        );

                        // Output tokens from result.usage are cumulative across
                        // all agentic rounds — correct for billing/display of
                        // "tokens generated this turn".
                        let final_output = usage
                            .as_ref()
                            .map(|u| u.output_tokens)
                            .unwrap_or(output_tokens);

                        // Pull cumulative billing values from result.usage.
                        // These represent the TOTAL tokens consumed across all
                        // sub-rounds — the right thing for cost calculation.
                        let (result_input, result_cc, result_cr) = usage
                            .as_ref()
                            .map(|u| {
                                (
                                    u.input_tokens,
                                    u.cache_creation_input_tokens,
                                    u.cache_read_input_tokens,
                                )
                            })
                            .unwrap_or((0, 0, 0));

                        // CRITICAL: result.usage is CUMULATIVE across rounds
                        // (verified from logs: num_turns=1 → 136K input, but
                        // num_turns=19 → 4.8M input — ~36× because each round
                        // re-includes the growing context AND tool/file content
                        // which is also pulled in cumulatively). It is the
                        // WRONG value for context-window display, which needs
                        // the size of the LAST round's prompt only.
                        //
                        // The per-round trackers (input_tokens, *_last) are
                        // populated by the Assistant envelope handler when
                        // qwen-cli ships per-round usage on its assistant
                        // messages. If those are present, use them. Otherwise,
                        // emit ZERO for context fields so tool_loop's
                        // calibration is skipped entirely and the local
                        // tiktoken estimate of context.token_count is kept
                        // (which matches what the user sees after restart).
                        let ctx_input = input_tokens;
                        let ctx_cache_creation = cache_creation_tokens_last;
                        let ctx_cache_read = cache_read_tokens_last;

                        // Billing accumulates across rounds for cost tracking.
                        // Prefer the cumulative tracker; if we never received
                        // per-round assistant usage, fall back to result.usage
                        // (which is also cumulative).
                        let billing_cache_creation = if cache_creation_tokens_billing > 0 {
                            cache_creation_tokens_billing
                        } else {
                            result_cc
                        };
                        let billing_cache_read = if cache_read_tokens_billing > 0 {
                            cache_read_tokens_billing
                        } else {
                            result_cr
                        };

                        tracing::info!(
                            "Qwen result envelope: cumulative result in={} cc={} cr={} \
                             out={} num_turns_seen | per-round LAST in={} cc={} cr={} \
                             (ctx: {})",
                            result_input,
                            result_cc,
                            result_cr,
                            final_output,
                            input_tokens,
                            cache_creation_tokens_last,
                            cache_read_tokens_last,
                            if ctx_input + ctx_cache_creation + ctx_cache_read > 0 {
                                "using per-round LAST"
                            } else {
                                "skipped — falling back to local tiktoken estimate"
                            },
                        );

                        let _ = tx
                            .send(Ok(StreamEvent::MessageDelta {
                                delta: MessageDelta {
                                    stop_reason: reason,
                                    stop_sequence: None,
                                },
                                usage: TokenUsage {
                                    input_tokens: ctx_input,
                                    output_tokens: final_output,
                                    cache_creation_tokens: ctx_cache_creation,
                                    cache_read_tokens: ctx_cache_read,
                                    billing_cache_creation,
                                    billing_cache_read,
                                },
                            }))
                            .await;

                        let _ = tx.send(Ok(StreamEvent::MessageStop)).await;
                        result_received = true;
                        break;
                    }

                    CliMessage::User { .. } => {
                        tracing::debug!("Qwen CLI → user turn (tool_result)");
                        if tx.send(Ok(StreamEvent::Ping)).await.is_err() {
                            break;
                        }
                    }

                    CliMessage::RateLimitEvent {} => {
                        tracing::warn!("Qwen CLI → rate_limit_event");
                        if tx.send(Ok(StreamEvent::Ping)).await.is_err() {
                            break;
                        }
                    }
                }
            }

            // Flush any trailing buffered text before synthesizing MessageStop,
            // so the final chunk lands in DB even on clean EOF or cancel.
            if !text_persist_buf.is_empty() {
                let _ = persist_tx.send(format!("{}\n\n", std::mem::take(&mut text_persist_buf)));
            }

            // Synthesize MessageStop on EOF if qwen never sent a `result` envelope.
            // This is the normal path for qwen CLI (it does not emit a result like
            // claude CLI does). Always emit so the agent loop sees a valid
            // stop_reason and doesn't retry the request.
            if started && !result_received {
                if current_block_started {
                    let _ = tx
                        .send(Ok(StreamEvent::ContentBlockStop {
                            index: completed_blocks + block_index_offset,
                        }))
                        .await;
                }
                tracing::info!(
                    "Qwen CLI EOF — synthesizing MessageStop (saw_tool_use={}, in={}, out={})",
                    saw_tool_use_in_message,
                    input_tokens,
                    output_tokens
                );
                let _ = tx
                    .send(Ok(StreamEvent::MessageDelta {
                        delta: MessageDelta {
                            stop_reason: Some(StopReason::EndTurn),
                            stop_sequence: None,
                        },
                        usage: TokenUsage {
                            input_tokens,
                            output_tokens,
                            cache_creation_tokens: cache_creation_tokens_last,
                            cache_read_tokens: cache_read_tokens_last,
                            billing_cache_creation: cache_creation_tokens_billing,
                            billing_cache_read: cache_read_tokens_billing,
                        },
                    }))
                    .await;
                let _ = tx.send(Ok(StreamEvent::MessageStop)).await;
            }

            let exit_status = child.wait().await;
            match &exit_status {
                Ok(status) if !status.success() => {
                    if !started {
                        let _ = tx
                            .send(Err(ProviderError::Internal(format!(
                                "qwen CLI exited with {} before producing any output",
                                status
                            ))))
                            .await;
                    }
                }
                Err(e) => {
                    tracing::error!("Failed to wait on qwen CLI: {}", e);
                }
                Ok(_) => {
                    if !started {
                        tracing::warn!(
                            "qwen CLI exited successfully but produced no stream events"
                        );
                    }
                }
            }
        });

        let stream = futures::stream::unfold(rx, |mut rx| async move {
            rx.recv().await.map(|item| (item, rx))
        });
        Ok(Box::pin(stream))
    }

    fn name(&self) -> &str {
        "qwen-cli"
    }

    fn default_model(&self) -> &str {
        &self.default_model
    }

    fn supported_models(&self) -> Vec<String> {
        vec![
            "qwen3-coder-plus".to_string(),
            "qwen3.5-plus".to_string(),
            "qwen3.6-plus".to_string(),
            "qwen3-coder-480a35".to_string(),
            "qwen3-coder-30ba3b".to_string(),
            "qwen3-max-2026-01-23".to_string(),
        ]
    }

    fn context_window(&self, _model: &str) -> Option<u32> {
        Some(256_000) // Qwen3-Coder supports 256k context
    }

    fn calculate_cost(&self, model: &str, input_tokens: u32, output_tokens: u32) -> f64 {
        crate::pricing::PricingConfig::load().calculate_cost(model, input_tokens, output_tokens)
    }

    fn supports_tools(&self) -> bool {
        true
    }

    fn supports_vision(&self) -> bool {
        false
    }

    fn cli_handles_tools(&self) -> bool {
        true
    }

    /// Qwen CLI is spawned cold on every turn with a fresh `--session-id`,
    /// and the entire message history is re-serialized into the `-p` prompt
    /// (see `build_prompt`). It has zero persistent state across our calls,
    /// so OpenCrabs must compact context itself to stay under the window.
    fn cli_manages_context(&self) -> bool {
        false
    }
}

// ── Helper functions (same as claude_cli) ──

fn cli_block_text(block: &CliContentBlock) -> &str {
    match block {
        CliContentBlock::Text { text } => text.as_str(),
        CliContentBlock::Thinking { thinking } => thinking.as_str(),
        _ => "",
    }
}

fn cli_empty_block(block: &CliContentBlock) -> ContentBlock {
    match block {
        CliContentBlock::Text { .. } => ContentBlock::Text {
            text: String::new(),
        },
        CliContentBlock::Thinking { .. } => ContentBlock::Thinking {
            thinking: String::new(),
            signature: None,
        },
        CliContentBlock::ToolUse { id, name, .. } => ContentBlock::ToolUse {
            id: id.clone(),
            name: normalize_cli_tool_name(name),
            input: serde_json::json!({}),
        },
        CliContentBlock::Unknown => ContentBlock::Text {
            text: String::new(),
        },
    }
}

fn cli_block_delta(block: &CliContentBlock, new_text: &str) -> ContentDelta {
    match block {
        CliContentBlock::Thinking { .. } => ContentDelta::ThinkingDelta {
            thinking: new_text.to_string(),
        },
        _ => ContentDelta::TextDelta {
            text: new_text.to_string(),
        },
    }
}

async fn emit_full_block(
    tx: &tokio::sync::mpsc::Sender<super::error::Result<StreamEvent>>,
    block: &CliContentBlock,
    index: usize,
) {
    let empty = cli_empty_block(block);
    let _ = tx
        .send(Ok(StreamEvent::ContentBlockStart {
            index,
            content_block: empty,
        }))
        .await;

    match block {
        CliContentBlock::Text { text } => {
            let _ = tx
                .send(Ok(StreamEvent::ContentBlockDelta {
                    index,
                    delta: ContentDelta::TextDelta { text: text.clone() },
                }))
                .await;
        }
        CliContentBlock::Thinking { thinking } => {
            let _ = tx
                .send(Ok(StreamEvent::ContentBlockDelta {
                    index,
                    delta: ContentDelta::ThinkingDelta {
                        thinking: thinking.clone(),
                    },
                }))
                .await;
        }
        CliContentBlock::ToolUse { input, .. } => {
            let input_str = serde_json::to_string(input).unwrap_or_default();
            let _ = tx
                .send(Ok(StreamEvent::ContentBlockDelta {
                    index,
                    delta: ContentDelta::InputJsonDelta {
                        partial_json: input_str,
                    },
                }))
                .await;
        }
        CliContentBlock::Unknown => {}
    }

    let _ = tx.send(Ok(StreamEvent::ContentBlockStop { index })).await;
}

fn normalize_cli_tool_name(name: &str) -> String {
    match name {
        // Claude-CLI style (capitalized)
        "Bash" => "bash".to_string(),
        "Read" => "read_file".to_string(),
        "Write" => "write_file".to_string(),
        "Edit" => "edit_file".to_string(),
        "Grep" => "grep".to_string(),
        "Glob" => "glob".to_string(),
        "LSP" => "lsp".to_string(),
        "WebSearch" => "web_search".to_string(),
        "WebFetch" => "http_request".to_string(),
        "Agent" => "agent".to_string(),
        "NotebookEdit" => "notebook_edit".to_string(),
        // Qwen Code CLI built-in tool names → opencrabs equivalents
        "run_shell_command" => "bash".to_string(),
        "search_file_content" => "grep".to_string(),
        "replace" => "edit_file".to_string(),
        "web_fetch" => "http_request".to_string(),
        "google_web_search" => "web_search".to_string(),
        "list_directory" => "glob".to_string(),
        "read_many_files" => "read_file".to_string(),
        other => other.to_string(),
    }
}

fn offset_block_index(event: StreamEvent, offset: usize) -> StreamEvent {
    if offset == 0 {
        return event;
    }
    match event {
        StreamEvent::ContentBlockStart {
            index,
            content_block,
        } => StreamEvent::ContentBlockStart {
            index: index + offset,
            content_block,
        },
        StreamEvent::ContentBlockDelta { index, delta } => StreamEvent::ContentBlockDelta {
            index: index + offset,
            delta,
        },
        StreamEvent::ContentBlockStop { index } => StreamEvent::ContentBlockStop {
            index: index + offset,
        },
        other => other,
    }
}

fn normalize_stream_event(event: StreamEvent) -> StreamEvent {
    match event {
        StreamEvent::ContentBlockStart {
            index,
            content_block: ContentBlock::ToolUse { id, name, input },
        } => StreamEvent::ContentBlockStart {
            index,
            content_block: ContentBlock::ToolUse {
                id,
                name: normalize_cli_tool_name(&name),
                input,
            },
        },
        other => other,
    }
}

/// Append a chunk directly to the latest assistant message for a session.
///
/// Uses the global DB pool published by `Database::connect` so qwen-cli can
/// persist streaming progress (text and tool markers) as they arrive, rather
/// than waiting for tool_loop.rs to batch-write at end of iteration. This
/// gives qwen-cli the same mid-run durability as non-CLI providers, which
/// persist per-tool at tool_loop.rs:2210.
///
/// Silent best-effort: any DB error is logged at debug level and swallowed.
/// Persistence failures must not break the streaming pipeline.
async fn persist_qwen_chunk(session_id: uuid::Uuid, chunk: &str) {
    let Some(pool) = crate::db::global_pool() else {
        return;
    };
    let session_str = session_id.to_string();
    let chunk_owned = chunk.to_string();
    let Ok(conn) = pool.get().await else {
        return;
    };
    let _ = conn
        .interact(move |conn| -> rusqlite::Result<()> {
            // Find the latest assistant message for this session.
            let mut stmt = conn.prepare(
                "SELECT id FROM messages \
                 WHERE session_id = ?1 AND role = 'assistant' \
                 ORDER BY created_at DESC LIMIT 1",
            )?;
            let id: Option<String> = stmt
                .query_row(rusqlite::params![session_str], |row| row.get(0))
                .ok();
            if let Some(id) = id {
                conn.execute(
                    "UPDATE messages SET content = content || ?1 WHERE id = ?2",
                    rusqlite::params![chunk_owned, id],
                )?;
            }
            Ok(())
        })
        .await;
}

/// Build a tools-v2 marker block for a single tool call in the same format
/// tool_loop.rs writes (see tool_loop.rs:2200-2212). Keeping the format
/// identical means session reload and TUI expansion code works unchanged.
fn format_tool_marker(name: &str, input: &serde_json::Value) -> String {
    let desc = format!("{}({})", name, shorten_tool_input(input));
    let entry = serde_json::json!({
        "d": desc,
        "s": true,
        "i": input,
    });
    format!(
        "\n<!-- tools-v2: [{}] -->\n",
        serde_json::to_string(&entry).unwrap_or_default()
    )
}

/// Short one-line summary of a tool's input for the `d` (description) field.
/// Mirrors the spirit of helpers::format_tool_summary without coupling across
/// module boundaries — the exact text is cosmetic, reload parses `i` for the
/// authoritative input.
fn shorten_tool_input(input: &serde_json::Value) -> String {
    let s = match input {
        serde_json::Value::Object(map) => {
            if let Some(cmd) = map.get("command").and_then(|v| v.as_str()) {
                cmd.to_string()
            } else if let Some(path) = map.get("file_path").and_then(|v| v.as_str()) {
                path.to_string()
            } else if let Some(pattern) = map.get("pattern").and_then(|v| v.as_str()) {
                pattern.to_string()
            } else if let Some(query) = map.get("query").and_then(|v| v.as_str()) {
                query.to_string()
            } else {
                serde_json::to_string(input).unwrap_or_default()
            }
        }
        _ => serde_json::to_string(input).unwrap_or_default(),
    };
    if s.len() > 120 {
        format!("{}", &s[..s.floor_char_boundary(120)])
    } else {
        s
    }
}