rsclaw 2026.5.1

AI Agent Engine Compatible with OpenClaw
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
//! ACP (Agent Client Protocol) tool handlers for OpenCode and Claude Code.
//!
//! Extracted from `runtime.rs` to keep the main agent loop file focused.
//! These methods live in a separate `impl AgentRuntime` block which Rust
//! allows across multiple files within the same crate.

use std::sync::Arc;
use std::time::Duration;

use anyhow::{Result, anyhow};
use serde_json::Value;
use futures::future::BoxFuture;

use super::registry::{AgentMessage, AgentReply};
use super::runtime::{AgentRuntime, RunContext, expand_tilde};
use crate::acp::notification::{Notification, NotificationPriority, NotificationSink};
use crate::channel::OutboundMessage;

/// A notification sink that forwards ACP notifications to a channel.
/// Used to send OpenCode/ClaudeCode tool progress notifications to the user.
struct ChannelNotifier {
    tx: tokio::sync::broadcast::Sender<OutboundMessage>,
    target_id: String,
    channel: String,
}

impl ChannelNotifier {
    fn new(tx: tokio::sync::broadcast::Sender<OutboundMessage>, target_id: String, channel: String) -> Self {
        Self { tx, target_id, channel }
    }
}

impl NotificationSink for ChannelNotifier {
    fn name(&self) -> &str {
        "channel"
    }

    fn priority_filter(&self) -> NotificationPriority {
        // Only forward HIGH priority notifications to user channel.
        // Medium/Low (tool progress, thoughts) stay in logs only.
        NotificationPriority::High
    }

    fn send(&self, notification: &Notification) -> BoxFuture<'_, Result<()>> {
        let text = format!(
            "**{}**\n\n{}",
            notification.title,
            notification.body
        );
        let msg = OutboundMessage {
            target_id: self.target_id.clone(),
            is_group: false,
            text,
            reply_to: None,
            images: vec![],
            files: vec![],
            channel: Some(self.channel.clone()),
            account: None,
        };
        let tx = self.tx.clone();
        Box::pin(async move {
            tx.send(msg).map(|_| ()).map_err(|e| anyhow!("channel notification failed: {}", e))
        })
    }
}

impl AgentRuntime {
    /// Get or create the OpenCode ACP client.
    /// Uses the agent's workspace directory as cwd for proper file
    /// organization.
    pub(crate) async fn get_opencode_client(&self) -> Result<crate::acp::client::AcpClient> {
        if let Some(client) = self.opencode_client.get() {
            return Ok(client.clone());
        }

        // Find opencode executable.
        // On Windows, npm packages have both shell scripts and .cmd wrappers.
        // Git Bash/MSYS2's which finds the shell script, but it internally calls
        // node with Unix-style paths like "/d/Program Files/nodejs/node" which
        // Windows subprocesses cannot understand. Use the .cmd wrapper instead.
        let command = if cfg!(target_os = "windows") {
            // On Windows, search for opencode.cmd directly in PATH directories.
            // Use Windows-native PATH format (split by ';' with backslash paths).
            // Git Bash may convert PATH to Unix format, so we also check common locations.
            let path_env = std::env::var("PATH").ok().unwrap_or_default();
            // PATH may be Unix-style (from Git Bash) or Windows-style
            let separator = if path_env.contains(';') { ';' } else { ':' };

            let cmd_found = path_env.split(separator).find_map(|dir| {
                // Convert Unix-style path to Windows format if needed
                let win_dir = if dir.starts_with('/') && dir.len() > 2 {
                    let parts: Vec<&str> = dir.splitn(3, '/').collect();
                    if parts.len() >= 3 && parts[0].is_empty() && parts[1].len() == 1 {
                        let drive = parts[1].to_uppercase();
                        let rest = parts[2];
                        format!("{}:\\{}", drive, rest.replace('/', "\\"))
                    } else {
                        dir.replace('/', "\\")
                    }
                } else {
                    dir.to_string()
                };

                let cmd_path = std::path::PathBuf::from(&win_dir).join("opencode.cmd");
                if cmd_path.exists() {
                    Some(cmd_path.to_string_lossy().to_string())
                } else {
                    None
                }
            });

            cmd_found
                .or_else(|| std::env::var("OPENCODE_PATH").ok())
                .unwrap_or_else(|| "opencode.cmd".to_string())
        } else {
            which::which("opencode")
                .map(|p| p.to_string_lossy().to_string())
                .or_else(|_| std::env::var("OPENCODE_PATH"))
                .unwrap_or_else(|_| "opencode".to_string())
        };

        // Use "acp" subcommand to start ACP protocol mode
        let args: Vec<&str> = vec!["acp"];

        tracing::info!(command = %command, args = ?args, "OpenCode: starting subprocess");

        // Use agent's workspace directory instead of current_dir
        // This ensures files are created in the right location
        let cwd = self
            .handle
            .config
            .workspace
            .as_deref()
            .or(self.config.agents.defaults.workspace.as_deref())
            .map(expand_tilde)
            .unwrap_or_else(|| crate::config::loader::base_dir().join("workspace"));
        // Convert to Windows native path string (avoid MSYS2/Git Bash Unix-style paths)
        // Don't use canonicalize() — it returns \\?\ prefix which breaks JSON serialization
        let cwd_str = if cfg!(target_os = "windows") {
            let abs_path = if cwd.is_absolute() {
                cwd.clone()
            } else {
                std::env::current_dir().unwrap_or_default().join(&cwd)
            };
            abs_path.to_string_lossy().to_string()
        } else {
            cwd.to_string_lossy().to_string()
        };

        tracing::info!(cwd = %cwd_str, "OpenCode: using workspace directory");

        // Get init timeout from config (default 600s)
        let init_timeout_secs = self
            .handle
            .config
            .opencode
            .as_ref()
            .and_then(|c| c.init_timeout_seconds);

        let client = crate::acp::client::AcpClient::spawn_with_timeout(
            &command,
            &args,
            Arc::new(crate::acp::client::DefaultAcpHandler),
            Arc::new(tokio::sync::Mutex::new(crate::acp::notification::NotificationManager::new())),
            init_timeout_secs,
        ).await?;
        client
            .initialize("rsclaw", option_env!("RSCLAW_BUILD_VERSION").unwrap_or("dev"))
            .await?;

        // Create session with model from config or environment
        let model = self
            .handle
            .config
            .opencode
            .as_ref()
            .and_then(|c| c.model.clone())
            .or_else(|| std::env::var("OPENCODE_MODEL").ok());
        let session_resp = client.create_session(&cwd_str, model.as_deref(), None).await?;

        tracing::info!(
            session_id = %session_resp.session_id,
            current_model = ?session_resp.models.as_ref().and_then(|m| m.available_models.first()).map(|m| &m.model_id),
            "OpenCode session created"
        );

        self.opencode_client.set(client.clone()).ok();
        Ok(client)
    }

    /// Tool handler for opencode ACP calls - runs asynchronously.
    /// Results are delivered via notification channel when complete.
    pub(crate) async fn tool_opencode(&self, ctx: &RunContext, args: Value) -> Result<Value> {
        let task = args["task"]
            .as_str()
            .ok_or_else(|| anyhow!("opencode tool requires 'task' argument"))?;

        tracing::info!(task = %task, "tool_opencode: starting");

        let lang = self.config.raw.gateway.as_ref()
            .and_then(|g| g.language.as_deref())
            .map(crate::i18n::resolve_lang)
            .unwrap_or("en");

        // Get notification sender early for error reporting
        let notif_tx = self.notification_tx.clone();
        let target_id = ctx.peer_id.clone();
        let channel_name = ctx.channel.clone();

        // Try to get client, send error notification if failed
        let client = match self.get_opencode_client().await {
            Ok(c) => c,
            Err(e) => {
                tracing::error!("tool_opencode: get_client failed: {}", e);
                if let Some(ref tx) = notif_tx {
                    let _ = tx.send(crate::channel::OutboundMessage {
                        target_id: target_id.clone(),
                        is_group: false,
                        text: crate::i18n::t_fmt("acp_start_failed", lang, &[("name", "OpenCode"), ("error", &e.to_string())]),
                        reply_to: None,
                        images: vec![],
                        files: vec![],
                        channel: Some(channel_name.clone()),
                        account: None,
                    });
                }
                return Err(e);
            }
        };

        // Add notification sink to client for real-time progress updates
        if let Some(ref tx) = notif_tx {
            let sink = Arc::new(ChannelNotifier::new(tx.clone(), target_id.clone(), channel_name.clone()));
            client.add_notification_sink(sink);
            tracing::info!("tool_opencode: added notification sink for {}", target_id);
        }

        let session_id = client.session_id().await.unwrap_or_default();
        let session_id_clone = session_id.clone();

        let task_str = task.to_string();

        // Send initial notification
        tracing::info!("tool_opencode: sending initial notification to {}", target_id);
        if let Some(ref tx) = notif_tx {
            let _ = tx.send(crate::channel::OutboundMessage {
                target_id: target_id.clone(),
                is_group: false,
                text: crate::i18n::t_fmt("acp_submitted", lang, &[("name", "OpenCode")]),
                reply_to: None,
                images: vec![],
                files: vec![],
                channel: Some(channel_name.clone()),
                account: None,
            });
        } else {
            tracing::warn!("tool_opencode: no notification_tx for initial notification");
        }

        // Spawn background task - collect events AND send prompt in parallel
        let notif_tx_bg = notif_tx.clone();
        let target_id_bg = target_id.clone();
        let channel_bg = channel_name.clone();
        let lang_bg = lang;
        // Clone agent's own inbox for result injection after completion.
        let self_tx = self.handle.tx.clone();
        let self_session = ctx.session_key.clone();
        let self_channel = ctx.channel.clone();
        let self_peer_id = ctx.peer_id.clone();
        let self_chat_id = ctx.chat_id.clone();
        tokio::spawn(async move {
            tracing::info!("tool_opencode: background task started");
            // Start event collection FIRST (in parallel with send_prompt)
            let mut event_rx = client.subscribe_events();
            let events = Arc::new(tokio::sync::Mutex::new(Vec::<String>::new()));
            let events_clone = Arc::clone(&events);

            // Event collection task - collects events for final summary, NO intermediate notifications
            let _event_collector = tokio::spawn(async move {
                loop {
                    match event_rx.recv().await {
                        Ok(event) => {
                            // Only collect tool call events for summary, skip thoughts (AgentThoughtChunk)
                            let event_str = match &event {
                                crate::acp::client::SessionEvent::ToolCallStarted {
                                    title, ..
                                } => {
                                    let s = format!("🔧 {}", title.as_deref().unwrap_or("tool"));
                                    tracing::info!("OpenCode event: {}", s);
                                    s
                                }
                                crate::acp::client::SessionEvent::ToolCallCompleted {
                                    result,
                                    ..
                                } => {
                                    let s = result
                                        .as_ref()
                                        .map(|r| {
                                            if r.chars().count() > 100 {
                                                let cutoff = r
                                                    .char_indices()
                                                    .nth(100)
                                                    .map(|(i, _)| i)
                                                    .unwrap_or(r.len());
                                                format!("{}...", &r[..cutoff])
                                            } else {
                                                format!("{}", r)
                                            }
                                        })
                                        .unwrap_or_default();
                                    tracing::info!("OpenCode event: {}", s);
                                    s
                                }
                                crate::acp::client::SessionEvent::ToolCallFailed {
                                    error, ..
                                } => {
                                    let s = format!("{}", error);
                                    tracing::info!("OpenCode event: {}", s);
                                    s
                                }
                                crate::acp::client::SessionEvent::AgentMessageChunk {
                                    content,
                                } => {
                                    // Log message chunks for visibility
                                    tracing::debug!("OpenCode message: {}", content);
                                    String::new()
                                }
                                // Skip AgentThoughtChunk - don't send thinking messages to user
                                _ => String::new(),
                            };

                            if !event_str.is_empty() {
                                events_clone.lock().await.push(event_str.clone());
                            }
                        }
                        Err(_) => break,
                    }
                }
            });

            // Send prompt (runs in parallel with event collection)
            tracing::info!("tool_opencode: sending prompt");
            let send_result = client.send_prompt(&task_str).await;

            // DON'T wait for event collector - it runs forever! Just get what we have so
            // far. The events collected during execution are already in
            // `events`.

            // Process the result — collect summary + files for both notification and agent re-inject.
            let mut result_summary = String::new();
            let mut result_files: Vec<(String, String, String)> = vec![];
            match send_result {
                Ok(resp) => {
                    tracing::info!(
                        "tool_opencode: send_prompt completed, stop_reason={:?}",
                        resp.stop_reason
                    );

                    let events_list = events.lock().await.clone();
                    let collected = client.get_collected_content().await;
                    tracing::info!(
                        "tool_opencode: events count={}, collected len={}",
                        events_list.len(),
                        collected.len()
                    );

                    // Get the final result content
                    let result_content = if !collected.is_empty() {
                        collected
                    } else if let Some(result) = resp.result {
                        result
                            .content
                            .iter()
                            .filter_map(|b| match b {
                                crate::acp::types::ContentBlock::Text { text } => {
                                    Some(text.clone())
                                }
                                _ => None,
                            })
                            .collect::<Vec<_>>()
                            .join("\n")
                    } else {
                        String::new()
                    };

                    // Build a concise summary instead of dumping all events
                    let tool_count = events_list.iter().filter(|e| e.starts_with("🔧")).count();
                    let status_icon = match resp.stop_reason {
                        crate::acp::types::StopReason::EndTurn => "",
                        crate::acp::types::StopReason::MaxTokens => "⚠️",
                        crate::acp::types::StopReason::Cancelled => "⏹️",
                        crate::acp::types::StopReason::Incomplete => "",
                    };

                    // Scan result_content for downloadable file paths (e.g. mp4 downloaded by opencode).
                    let notif_files: Vec<(String, String, String)> = {
                        let sendable_exts = [".mp4", ".mp3", ".zip", ".pdf", ".xlsx", ".docx", ".pptx", ".csv", ".tar.gz"];
                        let mut found = Vec::new();
                        for token in result_content.split_whitespace() {
                            let trimmed = token.trim_matches(|c: char| "\"'.,;:()[]{}".contains(c));
                            // Strip any leading non-path characters (e.g. Chinese prefix like "路径:")
                            // by finding the first '/' or '~' in the token.
                            let trimmed = if let Some(pos) = trimmed.find(|c| c == '/' || c == '~') {
                                &trimmed[pos..]
                            } else {
                                trimmed
                            };
                            let lower = trimmed.to_lowercase();
                            if sendable_exts.iter().any(|ext| lower.ends_with(ext)) {
                                let path = expand_tilde(trimmed);
                                if path.exists() {
                                    if let Ok(meta) = path.metadata() {
                                        if meta.len() <= 50_000_000 {
                                            let filename = path.file_name().unwrap_or_default().to_string_lossy().to_string();
                                            let mime = if lower.ends_with(".mp4") { "video/mp4" }
                                                else if lower.ends_with(".mp3") { "audio/mpeg" }
                                                else if lower.ends_with(".pdf") { "application/pdf" }
                                                else if lower.ends_with(".zip") || lower.ends_with(".tar.gz") { "application/zip" }
                                                else if lower.ends_with(".xlsx") { "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }
                                                else if lower.ends_with(".docx") { "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }
                                                else if lower.ends_with(".pptx") { "application/vnd.openxmlformats-officedocument.presentationml.presentation" }
                                                else { "text/csv" };
                                            let path_str = path.to_string_lossy().to_string();
                                            if !found.iter().any(|(_, _, p): &(String, String, String)| p == &path_str) {
                                                tracing::info!(path = %path_str, "tool_opencode: attaching file to notification");
                                                found.push((filename, mime.to_owned(), path_str));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        found
                    };

                    let summary = if !result_content.is_empty() {
                        // Show result, truncated if too long (character-safe truncation)
                        let truncated = if result_content.chars().count() > 2000 {
                            let cutoff = result_content
                                .char_indices()
                                .nth(2000)
                                .map(|(i, _)| i)
                                .unwrap_or(result_content.len());
                            crate::i18n::t_fmt("acp_truncated", lang_bg, &[("content", &result_content[..cutoff])])
                        } else {
                            result_content
                        };
                        crate::i18n::t_fmt("acp_done_result", lang_bg, &[
                            ("status", status_icon), ("name", "OpenCode"),
                            ("count", &tool_count.to_string()), ("result", &truncated),
                        ])
                    } else if tool_count > 0 {
                        crate::i18n::t_fmt("acp_done_summary", lang_bg, &[
                            ("status", status_icon), ("name", "OpenCode"),
                            ("count", &tool_count.to_string()), ("summary", &events_list.join("\n")),
                        ])
                    } else {
                        crate::i18n::t_fmt("acp_done_empty", lang_bg, &[
                            ("status", status_icon), ("name", "OpenCode"),
                        ])
                    };

                    // Store for agent re-inject after notification.
                    result_summary = summary.clone();
                    result_files = notif_files.clone();

                    // Send notification to user
                    tracing::info!(
                        summary_preview = %summary.chars().take(100).collect::<String>(),
                        files_count = notif_files.len(),
                        target = %target_id_bg,
                        channel = %channel_bg,
                        "tool_opencode: sending completion notification"
                    );
                    if let Some(ref tx) = notif_tx_bg {
                        match tx.send(crate::channel::OutboundMessage {
                            target_id: target_id_bg.clone(),
                            is_group: false,
                            text: summary,
                            reply_to: None,
                            images: vec![],
                            files: notif_files,
                            channel: Some(channel_bg.clone()),
                            account: None,
                        }) {
                            Ok(_) => {
                                tracing::info!("tool_opencode: notification sent successfully to {}", target_id_bg);
                            }
                            Err(e) => {
                                tracing::error!("tool_opencode: failed to send notification: {}", e);
                            }
                        }
                    } else {
                        tracing::warn!("tool_opencode: no notification channel available");
                    }
                }
                Err(e) => {
                    tracing::error!("tool_opencode: send_prompt failed: {}", e);
                    if let Some(ref tx) = notif_tx_bg {
                        tracing::info!("tool_opencode: sending error notification to {}", target_id_bg);
                        let _ = tx.send(crate::channel::OutboundMessage {
                            target_id: target_id_bg.clone(),
                            is_group: false,
                            text: crate::i18n::t_fmt("acp_error", lang_bg, &[("name", "OpenCode"), ("error", &e.to_string())]),
                            reply_to: None,
                            images: vec![],
                            files: vec![],
                            channel: Some(channel_bg.clone()),
                            account: None,
                        });
                    }
                }
            }
            tracing::info!("tool_opencode: background task finished");
            // IMPORTANT: DON'T await event_collector - it runs forever waiting
            // for more events The collected events are already in
            // `events` variable

            // Inject result back into main agent's inbox so it can act on the result
            // (e.g. send_file). This triggers a new agent turn.
            let file_paths: Vec<String> = result_files.iter().map(|(_, _, p)| p.clone()).collect();
            let inject_text = if file_paths.is_empty() {
                format!("[OpenCode completed] {}", if result_summary.is_empty() { "Task finished.".to_owned() } else { result_summary })
            } else {
                format!("[OpenCode completed] Files ready: {}. Please send them to the user with send_file.",
                    file_paths.join(", "))
            };
            let (reply_tx, reply_rx) = tokio::sync::oneshot::channel::<AgentReply>();
            let inject_msg = AgentMessage {
                session_key: self_session,
                text: inject_text,
                channel: self_channel.clone(),
                peer_id: self_peer_id,
                chat_id: self_chat_id,
                reply_tx,
                extra_tools: vec![],
                images: vec![],
                files: vec![],
                account: None,
            };
            if self_tx.send(inject_msg).await.is_err() {
                tracing::warn!("tool_opencode: failed to inject result back to agent inbox");
            } else {
                tracing::info!("tool_opencode: result injected back to agent, waiting for reply");
                // Wait for agent's reply and forward it (text + files) to user via notification.
                match tokio::time::timeout(Duration::from_secs(300), reply_rx).await {
                    Ok(Ok(reply)) => {
                        if !reply.text.is_empty() || !reply.files.is_empty() || !reply.images.is_empty() {
                            if let Some(ref tx) = notif_tx_bg {
                                let _ = tx.send(crate::channel::OutboundMessage {
                                    target_id: target_id_bg.clone(),
                                    is_group: false,
                                    text: reply.text,
                                    reply_to: None,
                                    images: reply.images,
                                    files: reply.files,
                                    channel: Some(self_channel),
                                    account: None,
                                });
                                tracing::info!("tool_opencode: forwarded agent reply to user");
                            }
                        }
                    }
                    Ok(Err(_)) => tracing::warn!("tool_opencode: reply channel dropped"),
                    Err(_) => tracing::warn!("tool_opencode: reply timed out after 300s"),
                }
            }
        });

        Ok(serde_json::json!({
            "output": crate::i18n::t_fmt("acp_queued", lang, &[("name", "OpenCode")]),
            "status": "submitted",
            "session_id": session_id_clone
        }))
    }

    // -----------------------------------------------------------------------
    // Claude Code ACP integration
    // -----------------------------------------------------------------------

    /// Get or create the Claude Code ACP client.
    /// Uses claude-agent-acp which wraps Claude Agent SDK with ACP protocol.
    pub(crate) async fn get_claudecode_client(&self) -> Result<crate::acp::client::AcpClient> {
        if let Some(client) = self.claudecode_client.get() {
            return Ok(client.clone());
        }

        // Find claude-agent-acp executable.
        // On Windows, prefer .cmd wrapper to avoid MSYS2 path issues (same as opencode)
        let (command, args) = if cfg!(target_os = "windows") {
            // Search PATH for claude-agent-acp.cmd directly
            let path_env = std::env::var("PATH").ok().unwrap_or_default();
            let separator = if path_env.contains(';') { ';' } else { ':' };

            let cmd_found = path_env.split(separator).find_map(|dir| {
                let win_dir = if dir.starts_with('/') && dir.len() > 2 {
                    let parts: Vec<&str> = dir.splitn(3, '/').collect();
                    if parts.len() >= 3 && parts[0].is_empty() && parts[1].len() == 1 {
                        let drive = parts[1].to_uppercase();
                        let rest = parts[2];
                        format!("{}:\\{}", drive, rest.replace('/', "\\"))
                    } else {
                        dir.replace('/', "\\")
                    }
                } else {
                    dir.to_string()
                };

                // Try .cmd first
                let cmd_path = std::path::PathBuf::from(&win_dir).join("claude-agent-acp.cmd");
                if cmd_path.exists() {
                    return Some((cmd_path.to_string_lossy().to_string(), vec![]));
                }
                // Then try without extension
                let bin_path = std::path::PathBuf::from(&win_dir).join("claude-agent-acp");
                if bin_path.exists() {
                    return Some((bin_path.to_string_lossy().to_string(), vec![]));
                }
                None
            });

            cmd_found.unwrap_or_else(|| ("claude-agent-acp.cmd".to_string(), vec![]))
        } else {
            // Non-Windows: use which::which
            if let Ok(path) = which::which("claude-agent-acp") {
                (path.to_string_lossy().to_string(), vec![])
            } else if let Ok(path) = std::env::var("CLAUDE_AGENT_ACP_PATH") {
                // If it's a .js file, run with node
                if path.ends_with(".js") {
                    ("node".to_string(), vec![path])
                } else {
                    (path, vec![])
                }
            } else {
                // Try common npm global install paths
                let npm_global = std::env::var("npm_config_prefix").ok();
                let js_path = npm_global
                    .as_ref()
                    .map(|p| {
                        let mut path = std::path::PathBuf::from(p);
                        path.push("node_modules");
                        path.push("@agentclientprotocol");
                        path.push("claude-agent-acp");
                        path.push("dist");
                        path.push("index.js");
                        path
                    })
                    .or_else(|| {
                        dirs_next::home_dir().map(|h| {
                            let mut path = h;
                            path.push(".npm-global");
                            path.push("node_modules");
                            path.push("@agentclientprotocol");
                            path.push("claude-agent-acp");
                            path.push("dist");
                            path.push("index.js");
                            path
                        })
                    })
                    .or_else(|| {
                        dirs_next::home_dir().map(|h| {
                            let mut path = h;
                            path.push("node_modules");
                            path.push("@agentclientprotocol");
                            path.push("claude-agent-acp");
                            path.push("dist");
                            path.push("index.js");
                            path
                        })
                    });

                match js_path {
                    Some(p) if p.exists() => {
                        // .js files need to be run with node
                        // Convert path to Windows native format (avoid MSYS2/Git Bash paths)
                        let path_str = if cfg!(target_os = "windows") {
                            p.canonicalize()
                                .map(|c| c.to_string_lossy().to_string())
                                .unwrap_or_else(|_| p.to_string_lossy().to_string())
                        } else {
                            p.to_string_lossy().to_string()
                        };
                        ("node".to_string(), vec![path_str])
                    }
                    _ => {
                        // Fallback - let spawn handle the error
                        ("claude-agent-acp".to_string(), vec![])
                    }
                }
            }
        };

        tracing::info!(command = %command, "Claude Code: starting subprocess");

        // Use agent's workspace directory
        let cwd = self
            .handle
            .config
            .workspace
            .as_deref()
            .or(self.config.agents.defaults.workspace.as_deref())
            .map(expand_tilde)
            .unwrap_or_else(|| crate::config::loader::base_dir().join("workspace"));
        // Convert to Windows native path string (avoid MSYS2/Git Bash Unix-style paths)
        // Don't use canonicalize() — it returns \\?\ prefix which breaks JSON serialization
        let cwd_str = if cfg!(target_os = "windows") {
            let abs_path = if cwd.is_absolute() {
                cwd.clone()
            } else {
                std::env::current_dir().unwrap_or_default().join(&cwd)
            };
            abs_path.to_string_lossy().to_string()
        } else {
            cwd.to_string_lossy().to_string()
        };

        tracing::info!(cwd = %cwd_str, args = ?args, "Claude Code: using workspace directory");

        // Get init timeout from config (default 600s)
        let init_timeout_secs = self
            .handle
            .config
            .claudecode
            .as_ref()
            .and_then(|c| c.init_timeout_seconds);

        let args_ref: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
        let client = crate::acp::client::AcpClient::spawn_with_timeout(
            &command,
            &args_ref,
            Arc::new(crate::acp::client::DefaultAcpHandler),
            Arc::new(tokio::sync::Mutex::new(crate::acp::notification::NotificationManager::new())),
            init_timeout_secs,
        ).await?;
        client
            .initialize("rsclaw", option_env!("RSCLAW_BUILD_VERSION").unwrap_or("dev"))
            .await?;

        // Create session with model if configured
        let model = self
            .handle
            .config
            .claudecode
            .as_ref()
            .and_then(|c| c.model.clone())
            .or_else(|| std::env::var("CLAUDE_MODEL").ok())
            .or_else(|| std::env::var("ANTHROPIC_MODEL").ok());
        tracing::info!(
            model = ?model,
            claudecode_config = ?self.handle.config.claudecode,
            "Claude Code: model configuration"
        );
        let session_resp = client.create_session(&cwd_str, model.as_deref(), None).await?;

        tracing::info!(
            session_id = %session_resp.session_id,
            "Claude Code session created"
        );

        // Set model explicitly after session creation (modelId in session/new doesn't switch model)
        if let Some(ref m) = model {
            tracing::info!(model = %m, "Claude Code: setting model after session creation");
            client.set_model(m).await?;
        }

        self.claudecode_client.set(client.clone()).ok();
        Ok(client)
    }

    /// Tool handler for Claude Code ACP calls - runs asynchronously.
    /// Results are delivered via notification channel when complete.
    pub(crate) async fn tool_claudecode(&self, ctx: &RunContext, args: Value) -> Result<Value> {
        let task = args["task"]
            .as_str()
            .ok_or_else(|| anyhow!("claudecode tool requires 'task' argument"))?;

        tracing::info!(task = %task, "tool_claudecode: starting");

        let lang = self.config.raw.gateway.as_ref()
            .and_then(|g| g.language.as_deref())
            .map(crate::i18n::resolve_lang)
            .unwrap_or("en");

        // Get notification sender early for error reporting
        let notif_tx = self.notification_tx.clone();
        let target_id = ctx.peer_id.clone();
        let channel_name = ctx.channel.clone();

        // Try to get client, send error notification if failed
        let client = match self.get_claudecode_client().await {
            Ok(c) => c,
            Err(e) => {
                tracing::error!("tool_claudecode: get_client failed: {}", e);
                if let Some(ref tx) = notif_tx {
                    let _ = tx.send(crate::channel::OutboundMessage {
                        target_id: target_id.clone(),
                        is_group: false,
                        text: crate::i18n::t_fmt("acp_start_failed", lang, &[("name", "Claude Code"), ("error", &e.to_string())]),
                        reply_to: None,
                        images: vec![],
                        files: vec![],
                        channel: Some(channel_name.clone()),
                        account: None,
                    });
                }
                return Err(e);
            }
        };

        // Add notification sink to client for real-time progress updates
        if let Some(ref tx) = notif_tx {
            let sink = Arc::new(ChannelNotifier::new(tx.clone(), target_id.clone(), channel_name.clone()));
            client.add_notification_sink(sink);
            tracing::info!("tool_claudecode: added notification sink for {}", target_id);
        }

        let session_id = client.session_id().await.unwrap_or_default();
        let session_id_clone = session_id.clone();

        let task_str = task.to_string();

        // Send initial notification
        tracing::info!("tool_claudecode: sending initial notification to {}", target_id);
        if let Some(ref tx) = notif_tx {
            let _ = tx.send(crate::channel::OutboundMessage {
                target_id: target_id.clone(),
                is_group: false,
                text: crate::i18n::t_fmt("acp_submitted", lang, &[("name", "Claude Code")]),
                reply_to: None,
                images: vec![],
                files: vec![],
                channel: Some(channel_name.clone()),
                account: None,
            });
        } else {
            tracing::warn!("tool_claudecode: no notification_tx for initial notification");
        }

        // Spawn background task - collect events AND send prompt in parallel
        let notif_tx_bg = notif_tx.clone();
        let target_id_bg = target_id.clone();
        let channel_bg = channel_name.clone();
        let lang_bg = lang;
        tokio::spawn(async move {
            tracing::info!("tool_claudecode: background task started");
            // Start event collection FIRST (in parallel with send_prompt)
            let mut event_rx = client.subscribe_events();
            let events = Arc::new(tokio::sync::Mutex::new(Vec::<String>::new()));
            let events_clone = Arc::clone(&events);

            // Event collection task - collects events for final summary, NO intermediate notifications
            let _event_collector = tokio::spawn(async move {
                loop {
                    match event_rx.recv().await {
                        Ok(event) => {
                            // Only collect tool call events for summary, skip thoughts (AgentThoughtChunk)
                            let event_str = match &event {
                                crate::acp::client::SessionEvent::ToolCallStarted {
                                    title, ..
                                } => {
                                    let s = format!("🔧 {}", title.as_deref().unwrap_or("tool"));
                                    tracing::info!("OpenCode event: {}", s);
                                    s
                                }
                                crate::acp::client::SessionEvent::ToolCallCompleted {
                                    result,
                                    ..
                                } => {
                                    let s = result
                                        .as_ref()
                                        .map(|r| {
                                            if r.chars().count() > 100 {
                                                let cutoff = r
                                                    .char_indices()
                                                    .nth(100)
                                                    .map(|(i, _)| i)
                                                    .unwrap_or(r.len());
                                                format!("{}...", &r[..cutoff])
                                            } else {
                                                format!("{}", r)
                                            }
                                        })
                                        .unwrap_or_default();
                                    tracing::info!("OpenCode event: {}", s);
                                    s
                                }
                                crate::acp::client::SessionEvent::ToolCallFailed {
                                    error, ..
                                } => {
                                    let s = format!("{}", error);
                                    tracing::info!("OpenCode event: {}", s);
                                    s
                                }
                                crate::acp::client::SessionEvent::AgentMessageChunk {
                                    content,
                                } => {
                                    // Log message chunks for visibility
                                    tracing::debug!("OpenCode message: {}", content);
                                    String::new()
                                }
                                // Skip AgentThoughtChunk - don't send thinking messages to user
                                _ => String::new(),
                            };

                            if !event_str.is_empty() {
                                events_clone.lock().await.push(event_str.clone());
                            }
                        }
                        Err(_) => break,
                    }
                }
            });

            // Send prompt (runs in parallel with event collection)
            tracing::info!("tool_claudecode: sending prompt");
            let send_result = client.send_prompt(&task_str).await;

            // Process the result
            match send_result {
                Ok(resp) => {
                    tracing::info!(
                        "tool_claudecode: send_prompt completed, stop_reason={:?}",
                        resp.stop_reason
                    );

                    let events_list = events.lock().await.clone();
                    let collected = client.get_collected_content().await;
                    tracing::info!(
                        "tool_claudecode: events count={}, collected len={}",
                        events_list.len(),
                        collected.len()
                    );

                    // Get the final result content
                    let result_content = if !collected.is_empty() {
                        collected
                    } else if let Some(result) = resp.result {
                        result
                            .content
                            .iter()
                            .filter_map(|b| match b {
                                crate::acp::types::ContentBlock::Text { text } => {
                                    Some(text.clone())
                                }
                                _ => None,
                            })
                            .collect::<Vec<_>>()
                            .join("\n")
                    } else {
                        String::new()
                    };

                    // Build a concise summary instead of dumping all events
                    let tool_count = events_list.iter().filter(|e| e.starts_with("🔧")).count();
                    let status_icon = match resp.stop_reason {
                        crate::acp::types::StopReason::EndTurn => "",
                        crate::acp::types::StopReason::MaxTokens => "⚠️",
                        crate::acp::types::StopReason::Cancelled => "⏹️",
                        crate::acp::types::StopReason::Incomplete => "",
                    };

                    // Scan result_content for downloadable file paths.
                    let notif_files: Vec<(String, String, String)> = {
                        let sendable_exts = [".mp4", ".mp3", ".zip", ".pdf", ".xlsx", ".docx", ".pptx", ".csv", ".tar.gz"];
                        let mut found = Vec::new();
                        for token in result_content.split_whitespace() {
                            let trimmed = token.trim_matches(|c: char| "\"'.,;:()[]{}".contains(c));
                            // Strip any leading non-path characters (e.g. Chinese prefix like "路径:")
                            // by finding the first '/' or '~' in the token.
                            let trimmed = if let Some(pos) = trimmed.find(|c| c == '/' || c == '~') {
                                &trimmed[pos..]
                            } else {
                                trimmed
                            };
                            let lower = trimmed.to_lowercase();
                            if sendable_exts.iter().any(|ext| lower.ends_with(ext)) {
                                let path = expand_tilde(trimmed);
                                if path.exists() {
                                    if let Ok(meta) = path.metadata() {
                                        if meta.len() <= 50_000_000 {
                                            let filename = path.file_name().unwrap_or_default().to_string_lossy().to_string();
                                            let mime = if lower.ends_with(".mp4") { "video/mp4" }
                                                else if lower.ends_with(".mp3") { "audio/mpeg" }
                                                else if lower.ends_with(".pdf") { "application/pdf" }
                                                else if lower.ends_with(".zip") || lower.ends_with(".tar.gz") { "application/zip" }
                                                else if lower.ends_with(".xlsx") { "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }
                                                else if lower.ends_with(".docx") { "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }
                                                else if lower.ends_with(".pptx") { "application/vnd.openxmlformats-officedocument.presentationml.presentation" }
                                                else { "text/csv" };
                                            let path_str = path.to_string_lossy().to_string();
                                            if !found.iter().any(|(_, _, p): &(String, String, String)| p == &path_str) {
                                                tracing::info!(path = %path_str, "tool_claudecode: attaching file to notification");
                                                found.push((filename, mime.to_owned(), path_str));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        found
                    };

                    let summary = if !result_content.is_empty() {
                        // Show result, truncated if too long (character-safe truncation)
                        let truncated = if result_content.chars().count() > 2000 {
                            let cutoff = result_content
                                .char_indices()
                                .nth(2000)
                                .map(|(i, _)| i)
                                .unwrap_or(result_content.len());
                            crate::i18n::t_fmt("acp_truncated", lang_bg, &[("content", &result_content[..cutoff])])
                        } else {
                            result_content
                        };
                        crate::i18n::t_fmt("acp_done_result", lang_bg, &[
                            ("status", status_icon), ("name", "Claude Code"),
                            ("count", &tool_count.to_string()), ("result", &truncated),
                        ])
                    } else if tool_count > 0 {
                        crate::i18n::t_fmt("acp_done_summary", lang_bg, &[
                            ("status", status_icon), ("name", "Claude Code"),
                            ("count", &tool_count.to_string()), ("summary", &events_list.join("\n")),
                        ])
                    } else {
                        crate::i18n::t_fmt("acp_done_empty", lang_bg, &[
                            ("status", status_icon), ("name", "Claude Code"),
                        ])
                    };

                    // Send notification to user
                    tracing::info!(
                        "tool_claudecode: sending completion notification, summary_len={}, files={}",
                        summary.len(), notif_files.len()
                    );
                    if let Some(ref tx) = notif_tx_bg {
                        match tx.send(crate::channel::OutboundMessage {
                            target_id: target_id_bg.clone(),
                            is_group: false,
                            text: summary,
                            reply_to: None,
                            images: vec![],
                            files: notif_files,
                            channel: Some(channel_bg.clone()),
                            account: None,
                        }) {
                            Ok(_) => {
                                tracing::info!("tool_claudecode: notification sent successfully to {}", target_id_bg)
                            }
                            Err(e) => {
                                tracing::error!("tool_claudecode: failed to send notification: {}", e)
                            }
                        }
                    } else {
                        tracing::warn!("tool_claudecode: no notification channel available");
                    }
                }
                Err(e) => {
                    tracing::error!("tool_claudecode: send_prompt failed: {}", e);
                    if let Some(ref tx) = notif_tx_bg {
                        tracing::info!("tool_claudecode: sending error notification to {}", target_id_bg);
                        let _ = tx.send(crate::channel::OutboundMessage {
                            target_id: target_id_bg.clone(),
                            is_group: false,
                            text: crate::i18n::t_fmt("acp_error", lang_bg, &[("name", "Claude Code"), ("error", &e.to_string())]),
                            reply_to: None,
                            images: vec![],
                            files: vec![],
                            channel: Some(channel_bg.clone()),
                            account: None,
                        });
                    }
                }
            }
            tracing::info!("tool_claudecode: background task finished");
            // DON'T await event_collector - it runs forever
        });

        Ok(serde_json::json!({
            "output": crate::i18n::t_fmt("acp_queued", lang, &[("name", "Claude Code")]),
            "status": "submitted",
            "session_id": session_id_clone
        }))
    }

    // -----------------------------------------------------------------------
    // Codex MCP integration
    // -----------------------------------------------------------------------

    /// Get or create the Codex MCP client.
    /// Uses Codex CLI's MCP server mode (codex mcp-server).
    pub(crate) async fn get_codex_client(&self) -> Result<crate::acp::CodexClient> {
        if let Some(client) = self.codex_client.get() {
            return Ok(client.clone());
        }

        // Find codex executable
        let command = self
            .handle
            .config
            .codex
            .as_ref()
            .and_then(|c| c.command.clone())
            .or_else(|| which::which("codex").map(|p| p.to_string_lossy().to_string()).ok())
            .or_else(|| std::env::var("CODEX_PATH").ok())
            .unwrap_or_else(|| "codex".to_string());

        // Use agent's workspace directory
        let cwd = self
            .handle
            .config
            .workspace
            .as_deref()
            .or(self.config.agents.defaults.workspace.as_deref())
            .map(expand_tilde)
            .unwrap_or_else(|| crate::config::loader::base_dir().join("workspace"));

        // Get model from config or environment
        let model = self
            .handle
            .config
            .codex
            .as_ref()
            .and_then(|c| c.model.clone())
            .or_else(|| std::env::var("CODEX_MODEL").ok());

        tracing::info!(command = %command, cwd = %cwd.display(), model = ?model, "Codex: starting MCP server");

        let client = crate::acp::CodexClient::spawn(cwd, Some(&command), model.as_deref()).await?;

        self.codex_client.set(client.clone()).ok();
        Ok(client)
    }

    /// Tool handler for Codex MCP calls - runs asynchronously.
    /// Results are delivered via notification channel when complete.
    /// Codex uses MCP protocol (not ACP), so it's simpler - no event streaming.
    pub(crate) async fn tool_codex(&self, ctx: &RunContext, args: Value) -> Result<Value> {
        let task = args["task"]
            .as_str()
            .ok_or_else(|| anyhow!("codex tool requires 'task' argument"))?;

        tracing::info!(task = %task, "tool_codex: starting");

        let lang = self.config.raw.gateway.as_ref()
            .and_then(|g| g.language.as_deref())
            .map(crate::i18n::resolve_lang)
            .unwrap_or("en");

        // Get notification sender early for error reporting
        let notif_tx = self.notification_tx.clone();
        let target_id = ctx.peer_id.clone();
        let channel_name = ctx.channel.clone();

        // Try to get client, send error notification if failed
        let client = match self.get_codex_client().await {
            Ok(c) => c,
            Err(e) => {
                tracing::error!("tool_codex: get_client failed: {}", e);
                if let Some(ref tx) = notif_tx {
                    let _ = tx.send(crate::channel::OutboundMessage {
                        target_id: target_id.clone(),
                        is_group: false,
                        text: crate::i18n::t_fmt("acp_start_failed", lang, &[("name", "Codex"), ("error", &e.to_string())]),
                        reply_to: None,
                        images: vec![],
                        files: vec![],
                        channel: Some(channel_name.clone()),
                        account: None,
                    });
                }
                return Err(e);
            }
        };

        let task_str = task.to_string();

        // Send initial notification
        if let Some(ref tx) = notif_tx {
            let _ = tx.send(crate::channel::OutboundMessage {
                target_id: target_id.clone(),
                is_group: false,
                text: crate::i18n::t_fmt("acp_submitted", lang, &[("name", "Codex")]),
                reply_to: None,
                images: vec![],
                files: vec![],
                channel: Some(channel_name.clone()),
                account: None,
            });
        }

        // Spawn background task
        let notif_tx_bg = notif_tx.clone();
        let target_id_bg = target_id.clone();
        let channel_bg = channel_name.clone();
        let lang_bg = lang;
        tokio::spawn(async move {
            tracing::info!("tool_codex: background task started, calling execute");

            let result = client.execute(&task_str).await;

            match result {
                Ok(codex_result) => {
                    tracing::info!(
                        thread_id = ?codex_result.thread_id,
                        content_len = codex_result.content.len(),
                        "tool_codex: execute completed"
                    );

                    // Build summary
                    let content = codex_result.content;
                    let truncated = if content.chars().count() > 2000 {
                        let cutoff = content
                            .char_indices()
                            .nth(2000)
                            .map(|(i, _)| i)
                            .unwrap_or(content.len());
                        crate::i18n::t_fmt("acp_truncated", lang_bg, &[("content", &content[..cutoff])])
                    } else {
                        content.clone()
                    };

                    let summary = if content.is_empty() {
                        crate::i18n::t_fmt("acp_done_empty", lang_bg, &[("status", ""), ("name", "Codex")])
                    } else {
                        crate::i18n::t_fmt("acp_done_result", lang_bg, &[
                            ("status", ""), ("name", "Codex"),
                            ("count", "0"), ("result", &truncated),
                        ])
                    };

                    // Send notification to user
                    if let Some(ref tx) = notif_tx_bg {
                        let _ = tx.send(crate::channel::OutboundMessage {
                            target_id: target_id_bg.clone(),
                            is_group: false,
                            text: summary,
                            reply_to: None,
                            images: vec![],
                            files: vec![],
                            channel: Some(channel_bg.clone()),
                            account: None,
                        });
                    }
                }
                Err(e) => {
                    tracing::error!("tool_codex: execute failed: {}", e);
                    if let Some(ref tx) = notif_tx_bg {
                        let _ = tx.send(crate::channel::OutboundMessage {
                            target_id: target_id_bg.clone(),
                            is_group: false,
                            text: crate::i18n::t_fmt("acp_error", lang_bg, &[("name", "Codex"), ("error", &e.to_string())]),
                            reply_to: None,
                            images: vec![],
                            files: vec![],
                            channel: Some(channel_bg.clone()),
                            account: None,
                        });
                    }
                }
            }
            tracing::info!("tool_codex: background task finished");
        });

        Ok(serde_json::json!({
            "output": crate::i18n::t_fmt("acp_queued", lang, &[("name", "Codex")]),
            "status": "submitted"
        }))
    }
}