recursive-agent 0.6.0

A minimal, orthogonal, self-improving coding agent kernel in Rust
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
//! `a2a_call` tool — invoke a remote A2A (Agent2Agent v1.0) agent.
//!
//! A2A is an open protocol (Linux Foundation / originally Google) that lets
//! agents built by different vendors delegate tasks to each other via HTTP+JSON.
//!
//! # Protocol overview
//!
//! 1. Client sends `POST {base_url}/message:send` with a `SendMessageRequest`.
//! 2. Server returns either a direct `Message` (synchronous) or a `Task` (async).
//! 3. If a `Task` is returned, the client polls `GET {base_url}/tasks/{id}` until
//!    the task reaches a terminal state (`COMPLETED`, `FAILED`, etc.).
//! 4. Artifacts from the completed task contain the final output.
//!
//! Reference: <https://a2a-protocol.org/v1.0.0/specification/>

use async_trait::async_trait;
use reqwest::header::{AUTHORIZATION, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::time::{Duration, Instant};

use crate::error::{Error, Result};
use crate::llm::ToolSpec;
use crate::tools::{Tool, ToolSideEffect};

// ---------------------------------------------------------------------------
// A2A data model (minimal subset for MVP)
// ---------------------------------------------------------------------------

/// A text, file, or data part inside a Message or Artifact.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Part {
    /// Plain text content. Other part types (file, data) are ignored for now.
    #[serde(default)]
    pub text: Option<String>,
}

/// An artifact produced by the remote agent.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Artifact {
    #[serde(default)]
    pub parts: Vec<Part>,
}

/// Task lifecycle state values from the A2A v1.0 spec.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TaskState {
    #[serde(rename = "TASK_STATE_SUBMITTED")]
    Submitted,
    #[serde(rename = "TASK_STATE_WORKING")]
    Working,
    #[serde(rename = "TASK_STATE_COMPLETED")]
    Completed,
    #[serde(rename = "TASK_STATE_FAILED")]
    Failed,
    #[serde(rename = "TASK_STATE_CANCELED")]
    Canceled,
    #[serde(rename = "TASK_STATE_REJECTED")]
    Rejected,
    /// Catch-all for unknown states (forward compatibility).
    #[serde(other)]
    Unknown,
}

impl TaskState {
    fn is_terminal(&self) -> bool {
        matches!(
            self,
            Self::Completed | Self::Failed | Self::Canceled | Self::Rejected | Self::Unknown
        )
    }

    fn as_str(&self) -> &'static str {
        match self {
            Self::Submitted => "TASK_STATE_SUBMITTED",
            Self::Working => "TASK_STATE_WORKING",
            Self::Completed => "TASK_STATE_COMPLETED",
            Self::Failed => "TASK_STATE_FAILED",
            Self::Canceled => "TASK_STATE_CANCELED",
            Self::Rejected => "TASK_STATE_REJECTED",
            Self::Unknown => "TASK_STATE_UNKNOWN",
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskStatus {
    pub state: TaskState,
}

/// A task returned by `POST /message:send` or `GET /tasks/{id}`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Task {
    pub id: String,
    pub status: TaskStatus,
    #[serde(default)]
    pub artifacts: Vec<Artifact>,
}

/// A direct message response (no task tracking needed).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageResponse {
    #[serde(default)]
    pub parts: Vec<Part>,
}

/// Top-level response from `POST /message:send`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SendMessageResponse {
    /// Agent returned a Task (async processing).
    HasTask { task: Task },
    /// Agent returned a direct Message.
    HasMessage { message: MessageResponse },
}

// ---------------------------------------------------------------------------
// Helper: extract text from a list of Parts
// ---------------------------------------------------------------------------

fn parts_to_text(parts: &[Part]) -> String {
    parts
        .iter()
        .filter_map(|p| p.text.as_deref())
        .collect::<Vec<_>>()
        .join("\n")
}

fn artifacts_to_text(artifacts: &[Artifact]) -> String {
    artifacts
        .iter()
        .flat_map(|a| a.parts.iter())
        .filter_map(|p| p.text.as_deref())
        .collect::<Vec<_>>()
        .join("\n")
}

// ---------------------------------------------------------------------------
// A2aCallTool
// ---------------------------------------------------------------------------

/// Tool that calls a remote A2A v1.0 agent and returns its text output.
pub struct A2aCallTool;

impl A2aCallTool {
    pub fn new() -> Self {
        Self
    }

    /// Build a `reqwest::Client` with sane timeouts.
    fn build_client() -> std::result::Result<reqwest::Client, reqwest::Error> {
        reqwest::Client::builder()
            .timeout(Duration::from_secs(30))
            .connect_timeout(Duration::from_secs(10))
            .build()
    }
}

impl Default for A2aCallTool {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Tool for A2aCallTool {
    fn spec(&self) -> ToolSpec {
        ToolSpec {
            name: "a2a_call".into(),
            description: "Invoke a remote A2A (Agent2Agent v1.0) agent and return its response. \
                           Supports both polling mode (default) and real-time SSE streaming mode. \
                           In polling mode the tool sends a message and polls the task until done. \
                           In streaming mode it connects to the server's SSE stream and accumulates \
                           artifact text as it arrives."
                .into(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "url": {
                        "type": "string",
                        "description": "Base URL of the A2A server (e.g. 'https://agent.example.com'). \
                                        Polling: posts to {url}/message:send and polls {url}/tasks/{id}. \
                                        Streaming: posts to {url}/message:stream."
                    },
                    "prompt": {
                        "type": "string",
                        "description": "The text message to send to the remote agent."
                    },
                    "authorization": {
                        "type": "string",
                        "description": "Optional Authorization header value (e.g. 'Bearer <token>'). \
                                        Omit for public agents."
                    },
                    "timeout_secs": {
                        "type": "integer",
                        "description": "Maximum seconds to wait for task completion (default: 60, max: 300). \
                                        In polling mode: polls every 2 seconds. \
                                        In streaming mode: max time to keep the SSE connection open.",
                        "default": 60
                    },
                    "streaming": {
                        "type": "boolean",
                        "description": "If true, use SSE streaming mode (POST {url}/message:stream). \
                                        The server must support A2A streaming. Default: false.",
                        "default": false
                    },
                    "async_mode": {
                        "type": "boolean",
                        "description": "If true, submit the task and return immediately with the task ID \
                                        without waiting for completion. Use a2a_task_check later to \
                                        retrieve the result (composable with schedule_wakeup). Default: false.",
                        "default": false
                    }
                },
                "required": ["url", "prompt"]
            }),
        }
    }

    fn side_effect_class(&self) -> ToolSideEffect {
        ToolSideEffect::External
    }

    async fn execute(&self, arguments: Value) -> Result<String> {
        let url = arguments["url"]
            .as_str()
            .ok_or_else(|| Error::BadToolArgs {
                name: "a2a_call".into(),
                message: "missing required parameter: url".into(),
            })?;
        let prompt = arguments["prompt"]
            .as_str()
            .ok_or_else(|| Error::BadToolArgs {
                name: "a2a_call".into(),
                message: "missing required parameter: prompt".into(),
            })?;
        let authorization = arguments["authorization"].as_str();
        let timeout_secs = arguments["timeout_secs"]
            .as_u64()
            .unwrap_or(60)
            .clamp(1, 300);
        let use_streaming = arguments["streaming"].as_bool().unwrap_or(false);
        let use_async = arguments["async_mode"].as_bool().unwrap_or(false);

        // Normalise base URL — strip trailing slash.
        let base = url.trim_end_matches('/');

        let client = Self::build_client().map_err(|e| Error::Tool {
            name: "a2a_call".into(),
            message: format!("failed to build HTTP client: {e}"),
        })?;

        if use_streaming {
            return execute_streaming(base, prompt, authorization, timeout_secs, &client).await;
        }

        // ── Step 1: POST /message:send ────────────────────────────────────
        let message_id = uuid::Uuid::new_v4().to_string();
        let send_url = format!("{base}/message:send");

        let mut req = client
            .post(&send_url)
            .header(CONTENT_TYPE, "application/a2a+json")
            .json(&json!({
                "message": {
                    "role": "ROLE_USER",
                    "parts": [{"text": prompt}],
                    "messageId": message_id
                }
            }));

        if let Some(auth) = authorization {
            req = req.header(AUTHORIZATION, auth);
        }

        let resp = req.send().await.map_err(|e| Error::Tool {
            name: "a2a_call".into(),
            message: format!("network error calling {send_url}: {e}"),
        })?;

        let status = resp.status();
        if !status.is_success() {
            let body = resp.text().await.unwrap_or_default();
            return Ok(format!("ERROR: HTTP {status} from {send_url}: {body}"));
        }

        let body_text = resp.text().await.map_err(|e| Error::Tool {
            name: "a2a_call".into(),
            message: format!("failed to read response body: {e}"),
        })?;

        let send_resp: SendMessageResponse =
            serde_json::from_str(&body_text).map_err(|e| Error::Tool {
                name: "a2a_call".into(),
                message: format!("invalid A2A response: {e}\nbody: {body_text}"),
            })?;

        // ── Step 2: handle synchronous message response ───────────────────
        match send_resp {
            SendMessageResponse::HasMessage { message } => {
                let text = parts_to_text(&message.parts);
                return Ok(if text.is_empty() {
                    "(empty response from agent)".to_string()
                } else {
                    text
                });
            }
            SendMessageResponse::HasTask { mut task } => {
                // ── Async mode: return task ID + background poll script ──────
                if use_async {
                    let task_id = &task.id;
                    let state = task.status.state.as_str();
                    // Build a shell one-liner the agent can pass to run_background.
                    // When the task reaches a terminal state the script exits, and
                    // run_event_loop detects the completed background job and starts
                    // a new turn with the output as its goal.
                    let poll_cmd = format!(
                        "end=$(($(date +%s)+{timeout_secs})); \
while [ $(date +%s) -lt $end ]; do \
  r=$(curl -sf '{base}/tasks/{task_id}' 2>/dev/null || echo '{{}}'); \
  s=$(echo \"$r\" | python3 -c \"import sys,json;print(json.load(sys.stdin).get('status',{{}}).get('state','UNKNOWN'))\" 2>/dev/null || echo UNKNOWN); \
  case \"$s\" in TASK_STATE_COMPLETED|TASK_STATE_FAILED|TASK_STATE_CANCELED|TASK_STATE_REJECTED) \
    echo \"A2A task {task_id} finished: $s\"; echo \"$r\"; exit 0;; \
  esac; sleep 2; done; \
echo \"A2A task {task_id} timed out after {timeout_secs}s\""
                    );
                    return Ok(format!(
                        "TASK_ID: {task_id}\nSTATE: {state}\n\n\
To poll in background and auto-trigger a new turn on completion, \
call run_background with:\n  command: {poll_cmd}\n  name: a2a-{task_id}\n\n\
When the background job completes, a new turn will start automatically with the result."
                    ));
                }

                // ── Step 3: poll until terminal state ─────────────────────
                let deadline = Instant::now() + Duration::from_secs(timeout_secs);
                let task_url = format!("{base}/tasks/{}", task.id);

                while !task.status.state.is_terminal() {
                    if Instant::now() >= deadline {
                        return Ok(format!(
                            "ERROR: task {} timed out after {}s (last state: {})",
                            task.id,
                            timeout_secs,
                            task.status.state.as_str()
                        ));
                    }

                    tokio::time::sleep(Duration::from_secs(2)).await;

                    let mut poll_req = client.get(&task_url);
                    if let Some(auth) = authorization {
                        poll_req = poll_req.header(AUTHORIZATION, auth);
                    }

                    let poll_resp = poll_req.send().await.map_err(|e| Error::Tool {
                        name: "a2a_call".into(),
                        message: format!("network error polling {task_url}: {e}"),
                    })?;

                    let poll_status = poll_resp.status();
                    if !poll_status.is_success() {
                        let err_body = poll_resp.text().await.unwrap_or_default();
                        return Ok(format!(
                            "ERROR: HTTP {poll_status} polling {task_url}: {err_body}"
                        ));
                    }

                    let poll_body = poll_resp.text().await.map_err(|e| Error::Tool {
                        name: "a2a_call".into(),
                        message: format!("failed to read poll response: {e}"),
                    })?;

                    let updated: Task =
                        serde_json::from_str(&poll_body).map_err(|e| Error::Tool {
                            name: "a2a_call".into(),
                            message: format!("invalid task poll response: {e}\nbody: {poll_body}"),
                        })?;

                    task = updated;
                }

                // ── Step 4: extract result ─────────────────────────────────
                match task.status.state {
                    TaskState::Completed => {
                        let text = artifacts_to_text(&task.artifacts);
                        Ok(if text.is_empty() {
                            "(task completed with no artifact text)".to_string()
                        } else {
                            text
                        })
                    }
                    other => Ok(format!(
                        "ERROR: task {} ended with state {}",
                        task.id,
                        other.as_str()
                    )),
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// SSE streaming implementation
// ---------------------------------------------------------------------------

/// Parse one SSE event block (multiple `key: value` lines separated by `\n`)
/// and extract any artifact text and terminal task state from it.
fn parse_sse_event(event_block: &str) -> (Option<String>, Option<TaskState>) {
    let mut data_lines: Vec<&str> = Vec::new();
    for line in event_block.lines() {
        if let Some(data) = line.strip_prefix("data:") {
            data_lines.push(data.trim());
        }
    }
    let data = data_lines.join("");
    if data.is_empty() {
        return (None, None);
    }

    let Ok(v): std::result::Result<Value, _> = serde_json::from_str(&data) else {
        return (None, None);
    };

    // Extract artifact text — handle both A2A REST and JSON-RPC response shapes.
    let mut text: Option<String> = None;
    let mut state: Option<TaskState> = None;

    // Shape 1: {"type":"TaskArtifactUpdateEvent","task":{...}}
    // Shape 2: {"result":{"kind":"artifact-update","artifact":{...}}}
    if let Some(artifacts) = v
        .pointer("/task/artifacts")
        .or_else(|| v.pointer("/result/artifact/parts"))
        .and_then(|a| a.as_array())
    {
        let extracted: String = artifacts
            .iter()
            .flat_map(|a| a["parts"].as_array().into_iter().flatten())
            .filter_map(|p| p["text"].as_str())
            .collect::<Vec<_>>()
            .join("");
        if !extracted.is_empty() {
            text = Some(extracted);
        }
    } else if let Some(part_text) = v
        .pointer("/result/artifact/parts/0/text")
        .and_then(|v| v.as_str())
    {
        text = Some(part_text.to_string());
    }

    // Extract state — handle both shapes.
    let raw_state = v
        .pointer("/task/status/state")
        .or_else(|| v.pointer("/result/status/state"))
        .and_then(|s| s.as_str());

    if let Some(s) = raw_state {
        if let Ok(ts) = serde_json::from_value::<TaskState>(Value::String(s.to_string())) {
            state = Some(ts);
        }
    }

    (text, state)
}

/// Execute `a2a_call` in SSE streaming mode.
///
/// POSTs to `{base}/message:stream` with `Accept: text/event-stream` and reads
/// the SSE stream until a terminal task state is received or the timeout expires.
async fn execute_streaming(
    base: &str,
    prompt: &str,
    authorization: Option<&str>,
    timeout_secs: u64,
    client: &reqwest::Client,
) -> Result<String> {
    let stream_url = format!("{base}/message:stream");
    let message_id = uuid::Uuid::new_v4().to_string();
    let deadline = Instant::now() + Duration::from_secs(timeout_secs);

    let mut req = client
        .post(&stream_url)
        .header(CONTENT_TYPE, "application/a2a+json")
        .header(reqwest::header::ACCEPT, "text/event-stream")
        .json(&json!({
            "message": {
                "role": "ROLE_USER",
                "parts": [{"text": prompt}],
                "messageId": message_id
            }
        }));

    if let Some(auth) = authorization {
        req = req.header(AUTHORIZATION, auth);
    }

    let resp = req.send().await.map_err(|e| Error::Tool {
        name: "a2a_call".into(),
        message: format!("network error calling {stream_url}: {e}"),
    })?;

    let status = resp.status();
    if !status.is_success() {
        let body = resp.text().await.unwrap_or_default();
        return Ok(format!("ERROR: HTTP {status} from {stream_url}: {body}"));
    }

    // Read SSE events chunk by chunk.
    let mut buffer = String::new();
    let mut accumulated_text = String::new();
    let mut final_state: Option<TaskState> = None;
    let mut resp = resp;

    loop {
        if Instant::now() >= deadline {
            let suffix = if accumulated_text.is_empty() {
                "(no text received before timeout)".to_string()
            } else {
                accumulated_text.clone()
            };
            return Ok(format!(
                "ERROR: SSE stream timed out after {timeout_secs}s. Partial output:\n{suffix}"
            ));
        }

        let chunk = tokio::time::timeout(Duration::from_secs(5), resp.chunk())
            .await
            .map_err(|_| Error::Tool {
                name: "a2a_call".into(),
                message: "SSE chunk read timed out (5s idle)".into(),
            })?
            .map_err(|e| Error::Tool {
                name: "a2a_call".into(),
                message: format!("SSE read error: {e}"),
            })?;

        let Some(bytes) = chunk else {
            // Connection closed by server.
            break;
        };

        if let Ok(text_chunk) = std::str::from_utf8(&bytes) {
            buffer.push_str(text_chunk);
        }

        // Process complete SSE event blocks (separated by \n\n or \r\n\r\n).
        while let Some(pos) = buffer.find("\n\n").or_else(|| buffer.find("\r\n\r\n")) {
            let sep_len = if buffer[pos..].starts_with("\r\n\r\n") {
                4
            } else {
                2
            };
            let event_block = buffer[..pos].to_string();
            buffer.drain(..pos + sep_len);

            let (text, state) = parse_sse_event(&event_block);
            if let Some(t) = text {
                accumulated_text.push_str(&t);
            }
            if let Some(s) = state {
                if s.is_terminal() {
                    final_state = Some(s);
                    break;
                }
            }
        }

        if final_state.is_some() {
            break;
        }
    }

    match final_state {
        Some(TaskState::Completed) | None => {
            if accumulated_text.is_empty() {
                Ok("(task completed with no artifact text)".to_string())
            } else {
                Ok(accumulated_text)
            }
        }
        Some(other) => Ok(format!(
            "ERROR: task ended with state {} (partial output: {})",
            other.as_str(),
            accumulated_text
        )),
    }
}

// ---------------------------------------------------------------------------
// Agent Card data model
// ---------------------------------------------------------------------------

/// Capability flags from the A2A Agent Card.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AgentCapabilities {
    /// Whether the agent supports streaming (SSE) via `sendSubscribe`.
    #[serde(default)]
    pub streaming: bool,
    /// Whether the agent supports push-notification callbacks.
    #[serde(rename = "pushNotifications", default)]
    pub push_notifications: bool,
}

/// One skill entry in the Agent Card.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentSkill {
    pub id: String,
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub description: String,
}

/// Authentication scheme described in the Agent Card.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentAuthentication {
    pub schemes: Vec<String>,
}

/// Subset of the A2A v1.0 Agent Card returned by `GET /.well-known/agent.json`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentCard {
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub capabilities: AgentCapabilities,
    #[serde(default)]
    pub skills: Vec<AgentSkill>,
    pub authentication: Option<AgentAuthentication>,
}

impl AgentCard {
    /// Return a concise human-readable summary of this Agent Card.
    pub fn summary(&self) -> String {
        let mut lines: Vec<String> = Vec::new();
        let name = if self.name.is_empty() {
            "<unnamed>"
        } else {
            &self.name
        };
        lines.push(format!("Agent: {name}"));
        if !self.description.is_empty() {
            lines.push(format!("Description: {}", self.description));
        }
        lines.push(format!(
            "Streaming: {}",
            if self.capabilities.streaming {
                "supported"
            } else {
                "not supported"
            }
        ));
        lines.push(format!(
            "Push notifications: {}",
            if self.capabilities.push_notifications {
                "supported"
            } else {
                "not supported"
            }
        ));
        if let Some(auth) = &self.authentication {
            lines.push(format!("Auth: {}", auth.schemes.join(", ")));
        } else {
            lines.push("Auth: none".into());
        }
        if !self.skills.is_empty() {
            lines.push("Skills:".into());
            for skill in &self.skills {
                let desc = if skill.description.is_empty() {
                    String::new()
                } else {
                    format!(": {}", skill.description)
                };
                lines.push(format!("  - {}{}", skill.name, desc));
            }
        }
        lines.join("\n")
    }
}

// ---------------------------------------------------------------------------
// A2aCardTool
// ---------------------------------------------------------------------------

/// Tool that fetches and summarises a remote A2A Agent Card
/// (`GET /.well-known/agent.json`).
pub struct A2aCardTool;

impl A2aCardTool {
    pub fn new() -> Self {
        Self
    }
}

impl Default for A2aCardTool {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Tool for A2aCardTool {
    fn spec(&self) -> ToolSpec {
        ToolSpec {
            name: "a2a_card".into(),
            description: "Fetch and display the Agent Card of a remote A2A v1.0 agent. \
                           The card describes the agent's capabilities (streaming, push \
                           notifications), authentication scheme, and available skills. \
                           Call this before a2a_call to discover what the agent supports."
                .into(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "url": {
                        "type": "string",
                        "description": "Base URL of the A2A server (e.g. 'https://agent.example.com'). \
                                        The tool fetches {url}/.well-known/agent.json."
                    },
                    "authorization": {
                        "type": "string",
                        "description": "Optional Authorization header value (e.g. 'Bearer <token>')."
                    }
                },
                "required": ["url"]
            }),
        }
    }

    fn side_effect_class(&self) -> ToolSideEffect {
        ToolSideEffect::External
    }

    async fn execute(&self, arguments: Value) -> Result<String> {
        let url = arguments["url"]
            .as_str()
            .ok_or_else(|| Error::BadToolArgs {
                name: "a2a_card".into(),
                message: "missing required parameter: url".into(),
            })?;
        let authorization = arguments["authorization"].as_str();

        let base = url.trim_end_matches('/');
        let card_url = format!("{base}/.well-known/agent.json");

        let client = A2aCallTool::build_client().map_err(|e| Error::Tool {
            name: "a2a_card".into(),
            message: format!("failed to build HTTP client: {e}"),
        })?;

        let mut req = client.get(&card_url);
        if let Some(auth) = authorization {
            req = req.header(AUTHORIZATION, auth);
        }

        let resp = req.send().await.map_err(|e| Error::Tool {
            name: "a2a_card".into(),
            message: format!("network error fetching {card_url}: {e}"),
        })?;

        let status = resp.status();
        if !status.is_success() {
            let body = resp.text().await.unwrap_or_default();
            return Ok(format!("ERROR: HTTP {status} from {card_url}: {body}"));
        }

        let body_text = resp.text().await.map_err(|e| Error::Tool {
            name: "a2a_card".into(),
            message: format!("failed to read response body: {e}"),
        })?;

        let card: AgentCard = serde_json::from_str(&body_text).map_err(|e| Error::Tool {
            name: "a2a_card".into(),
            message: format!("invalid Agent Card JSON: {e}\nbody: {body_text}"),
        })?;

        Ok(card.summary())
    }
}

// ---------------------------------------------------------------------------
// A2aTaskCheckTool
// ---------------------------------------------------------------------------

/// Tool that checks the current state and artifacts of an A2A task.
///
/// Designed to be used together with `a2a_call` in `async_mode: true`, allowing
/// the agent to submit a long-running task and check on it later, optionally
/// combined with `schedule_wakeup` for periodic polling.
pub struct A2aTaskCheckTool;

impl A2aTaskCheckTool {
    pub fn new() -> Self {
        Self
    }
}

impl Default for A2aTaskCheckTool {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Tool for A2aTaskCheckTool {
    fn spec(&self) -> ToolSpec {
        ToolSpec {
            name: "a2a_task_check".into(),
            description: "Check the current status and artifacts of a previously submitted A2A task. \
                           Useful for one-off manual inspection. The preferred pattern for automatic \
                           async polling is: use a2a_call with async_mode=true (which provides a \
                           ready-to-run shell command), pass that command to run_background, and let \
                           run_event_loop auto-trigger a new turn when the background job completes."
                .into(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "url": {
                        "type": "string",
                        "description": "Base URL of the A2A server (same url used in a2a_call)."
                    },
                    "task_id": {
                        "type": "string",
                        "description": "Task ID returned by a2a_call in async_mode (the value after 'TASK_ID: ')."
                    },
                    "authorization": {
                        "type": "string",
                        "description": "Optional Authorization header value (e.g. 'Bearer <token>')."
                    }
                },
                "required": ["url", "task_id"]
            }),
        }
    }

    fn side_effect_class(&self) -> ToolSideEffect {
        ToolSideEffect::External
    }

    async fn execute(&self, arguments: Value) -> Result<String> {
        let url = arguments["url"]
            .as_str()
            .ok_or_else(|| Error::BadToolArgs {
                name: "a2a_task_check".into(),
                message: "missing required parameter: url".into(),
            })?;
        let task_id = arguments["task_id"]
            .as_str()
            .ok_or_else(|| Error::BadToolArgs {
                name: "a2a_task_check".into(),
                message: "missing required parameter: task_id".into(),
            })?;
        let authorization = arguments["authorization"].as_str();

        let base = url.trim_end_matches('/');
        let task_url = format!("{base}/tasks/{task_id}");

        let client = A2aCallTool::build_client().map_err(|e| Error::Tool {
            name: "a2a_task_check".into(),
            message: format!("failed to build HTTP client: {e}"),
        })?;

        let mut req = client.get(&task_url);
        if let Some(auth) = authorization {
            req = req.header(AUTHORIZATION, auth);
        }

        let resp = req.send().await.map_err(|e| Error::Tool {
            name: "a2a_task_check".into(),
            message: format!("network error fetching {task_url}: {e}"),
        })?;

        let status = resp.status();
        if !status.is_success() {
            let body = resp.text().await.unwrap_or_default();
            return Ok(format!("ERROR: HTTP {status} from {task_url}: {body}"));
        }

        let body_text = resp.text().await.map_err(|e| Error::Tool {
            name: "a2a_task_check".into(),
            message: format!("failed to read response body: {e}"),
        })?;

        let task: Task = serde_json::from_str(&body_text).map_err(|e| Error::Tool {
            name: "a2a_task_check".into(),
            message: format!("invalid task JSON: {e}\nbody: {body_text}"),
        })?;

        let state_str = task.status.state.as_str();
        let artifact_text = artifacts_to_text(&task.artifacts);

        if task.status.state.is_terminal() {
            if artifact_text.is_empty() {
                Ok(format!("STATE: {state_str}\n(no artifact text)"))
            } else {
                Ok(format!("STATE: {state_str}\n{artifact_text}"))
            }
        } else {
            Ok(format!(
                "STATE: {state_str}\n(task not yet complete; check again later)"
            ))
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::{Read, Write};
    use std::net::TcpListener;
    use std::sync::{Arc, Mutex};

    /// Spawn a one-shot mock HTTP server on an ephemeral port.
    /// `response_body` is the raw HTTP response (including headers) the server sends.
    fn spawn_mock_server(response_body: &'static str) -> std::net::SocketAddr {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        std::thread::spawn(move || {
            if let Ok((mut stream, _)) = listener.accept() {
                let mut buf = [0u8; 8192];
                let _ = stream.read(&mut buf);
                let _ = stream.write_all(response_body.as_bytes());
                let _ = stream.flush();
            }
        });
        addr
    }

    /// Spawn a mock server that returns different responses for each request.
    /// Requests are served in order; extras after the list result in 500.
    fn spawn_sequential_mock_server(responses: Vec<&'static str>) -> std::net::SocketAddr {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        let responses = Arc::new(Mutex::new(responses.into_iter()));
        std::thread::spawn(move || {
            for _ in 0..100 {
                if let Ok((mut stream, _)) = listener.accept() {
                    let mut buf = [0u8; 8192];
                    let _ = stream.read(&mut buf);
                    let response = responses
                        .lock()
                        .unwrap()
                        .next()
                        .unwrap_or("HTTP/1.1 500 Internal Server Error\r\nContent-Length: 5\r\nConnection: close\r\n\r\nerror");
                    let _ = stream.write_all(response.as_bytes());
                    let _ = stream.flush();
                }
            }
        });
        // Give the server a moment to start
        std::thread::sleep(std::time::Duration::from_millis(20));
        addr
    }

    fn make_json_response(body: &str) -> String {
        format!(
            "HTTP/1.1 200 OK\r\nContent-Type: application/a2a+json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
            body.len(),
            body
        )
    }

    // ── Unit tests for data model helpers ─────────────────────────────────

    #[test]
    fn parts_to_text_concatenates_text_parts() {
        let parts = vec![
            Part {
                text: Some("Hello".into()),
            },
            Part { text: None },
            Part {
                text: Some(" World".into()),
            },
        ];
        assert_eq!(parts_to_text(&parts), "Hello\n World");
    }

    #[test]
    fn task_state_is_terminal() {
        assert!(TaskState::Completed.is_terminal());
        assert!(TaskState::Failed.is_terminal());
        assert!(TaskState::Canceled.is_terminal());
        assert!(TaskState::Rejected.is_terminal());
        assert!(!TaskState::Submitted.is_terminal());
        assert!(!TaskState::Working.is_terminal());
    }

    #[test]
    fn missing_prompt_returns_bad_tool_args_error() {
        let rt = tokio::runtime::Runtime::new().unwrap();
        let tool = A2aCallTool::new();
        let err = rt
            .block_on(tool.execute(json!({"url": "http://localhost"})))
            .unwrap_err();
        assert!(matches!(err, Error::BadToolArgs { .. }));
    }

    #[test]
    fn missing_url_returns_bad_tool_args_error() {
        let rt = tokio::runtime::Runtime::new().unwrap();
        let tool = A2aCallTool::new();
        let err = rt
            .block_on(tool.execute(json!({"prompt": "hi"})))
            .unwrap_err();
        assert!(matches!(err, Error::BadToolArgs { .. }));
    }

    #[tokio::test]
    async fn immediate_message_response_returns_text() {
        let body = r#"{"message":{"parts":[{"text":"The weather is sunny."}]}}"#;
        let raw_resp = Box::leak(make_json_response(body).into_boxed_str());
        let addr = spawn_mock_server(raw_resp);
        tokio::time::sleep(Duration::from_millis(30)).await;

        let tool = A2aCallTool::new();
        let result = tool
            .execute(json!({
                "url": format!("http://{addr}"),
                "prompt": "What is the weather?"
            }))
            .await
            .unwrap();

        assert_eq!(result, "The weather is sunny.");
    }

    #[tokio::test]
    async fn completed_task_response_returns_artifact_text() {
        let body = r#"{"task":{"id":"t1","status":{"state":"TASK_STATE_COMPLETED"},"artifacts":[{"parts":[{"text":"Task done!"}]}]}}"#;
        let raw_resp = Box::leak(make_json_response(body).into_boxed_str());
        let addr = spawn_mock_server(raw_resp);
        tokio::time::sleep(Duration::from_millis(30)).await;

        let tool = A2aCallTool::new();
        let result = tool
            .execute(json!({
                "url": format!("http://{addr}"),
                "prompt": "Do the task"
            }))
            .await
            .unwrap();

        assert_eq!(result, "Task done!");
    }

    #[tokio::test]
    async fn working_task_polled_to_completion() {
        // First response: WORKING task
        let working_resp = make_json_response(
            r#"{"task":{"id":"t2","status":{"state":"TASK_STATE_WORKING"},"artifacts":[]}}"#,
        );
        // Second response (GET /tasks/t2): COMPLETED task
        let completed_resp = make_json_response(
            r#"{"id":"t2","status":{"state":"TASK_STATE_COMPLETED"},"artifacts":[{"parts":[{"text":"Polling done"}]}]}"#,
        );
        let working_resp: &'static str = Box::leak(working_resp.into_boxed_str());
        let completed_resp: &'static str = Box::leak(completed_resp.into_boxed_str());

        let addr = spawn_sequential_mock_server(vec![working_resp, completed_resp]);

        let tool = A2aCallTool::new();
        let result = tool
            .execute(json!({
                "url": format!("http://{addr}"),
                "prompt": "Do the task",
                "timeout_secs": 30
            }))
            .await
            .unwrap();

        assert_eq!(result, "Polling done");
    }

    #[tokio::test]
    async fn failed_task_returns_error_string() {
        let body = r#"{"task":{"id":"t3","status":{"state":"TASK_STATE_FAILED"},"artifacts":[]}}"#;
        let raw_resp = Box::leak(make_json_response(body).into_boxed_str());
        let addr = spawn_mock_server(raw_resp);
        tokio::time::sleep(Duration::from_millis(30)).await;

        let tool = A2aCallTool::new();
        let result = tool
            .execute(json!({
                "url": format!("http://{addr}"),
                "prompt": "Do the task"
            }))
            .await
            .unwrap();

        assert!(
            result.starts_with("ERROR: task t3 ended with state TASK_STATE_FAILED"),
            "got: {result}"
        );
    }

    #[tokio::test]
    async fn http_error_returns_error_string() {
        let raw_resp = "HTTP/1.1 503 Service Unavailable\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndown";
        let addr = spawn_mock_server(raw_resp);
        tokio::time::sleep(Duration::from_millis(30)).await;

        let tool = A2aCallTool::new();
        let result = tool
            .execute(json!({
                "url": format!("http://{addr}"),
                "prompt": "ping"
            }))
            .await
            .unwrap();

        assert!(result.starts_with("ERROR: HTTP"), "got: {result}");
    }

    // ── A2aCardTool tests ─────────────────────────────────────────────────

    #[test]
    fn agent_card_summary_full() {
        let card = AgentCard {
            name: "Weather Bot".into(),
            description: "Provides forecasts.".into(),
            capabilities: AgentCapabilities {
                streaming: true,
                push_notifications: false,
            },
            skills: vec![AgentSkill {
                id: "s1".into(),
                name: "get_weather".into(),
                description: "Get current weather.".into(),
            }],
            authentication: Some(AgentAuthentication {
                schemes: vec!["Bearer".into()],
            }),
        };
        let summary = card.summary();
        assert!(summary.contains("Agent: Weather Bot"), "{summary}");
        assert!(summary.contains("Streaming: supported"), "{summary}");
        assert!(
            summary.contains("Push notifications: not supported"),
            "{summary}"
        );
        assert!(summary.contains("Auth: Bearer"), "{summary}");
        assert!(summary.contains("get_weather"), "{summary}");
    }

    #[test]
    fn agent_card_summary_empty_name_uses_unnamed() {
        let card = AgentCard {
            name: String::new(),
            description: String::new(),
            capabilities: AgentCapabilities::default(),
            skills: vec![],
            authentication: None,
        };
        let summary = card.summary();
        assert!(summary.contains("Agent: <unnamed>"), "{summary}");
        assert!(summary.contains("Auth: none"), "{summary}");
    }

    #[tokio::test]
    async fn a2a_card_parses_valid_agent_card() {
        let body = r#"{
            "name": "Echo Agent",
            "description": "Echoes your message.",
            "capabilities": {"streaming": false, "pushNotifications": false},
            "skills": [{"id": "echo", "name": "echo", "description": "Echo text."}]
        }"#;
        let raw_resp = Box::leak(make_json_response(body).into_boxed_str());
        let addr = spawn_mock_server(raw_resp);
        tokio::time::sleep(Duration::from_millis(30)).await;

        let tool = A2aCardTool::new();
        let result = tool
            .execute(json!({"url": format!("http://{addr}")}))
            .await
            .unwrap();

        assert!(result.contains("Echo Agent"), "{result}");
        assert!(result.contains("Streaming: not supported"), "{result}");
        assert!(result.contains("echo"), "{result}");
    }

    #[tokio::test]
    async fn a2a_card_returns_error_on_http_404() {
        let raw_resp =
            "HTTP/1.1 404 Not Found\r\nContent-Length: 9\r\nConnection: close\r\n\r\nNot Found";
        let addr = spawn_mock_server(raw_resp);
        tokio::time::sleep(Duration::from_millis(30)).await;

        let tool = A2aCardTool::new();
        let result = tool
            .execute(json!({"url": format!("http://{addr}")}))
            .await
            .unwrap();

        assert!(result.starts_with("ERROR: HTTP 404"), "{result}");
    }

    // ── SSE streaming tests ────────────────────────────────────────────────

    #[test]
    fn parse_sse_event_extracts_artifact_text() {
        let event = r#"data: {"type":"TaskArtifactUpdateEvent","task":{"id":"t1","status":{"state":"TASK_STATE_WORKING"},"artifacts":[{"parts":[{"text":"Hello world"}],"index":0}]}}"#;
        let (text, state) = parse_sse_event(event);
        assert_eq!(text.as_deref(), Some("Hello world"));
        assert!(state.is_none() || matches!(state, Some(TaskState::Working)));
    }

    #[test]
    fn parse_sse_event_extracts_terminal_state() {
        let event = r#"data: {"type":"TaskStatusUpdateEvent","task":{"id":"t1","status":{"state":"TASK_STATE_COMPLETED"},"artifacts":[]}}"#;
        let (text, state) = parse_sse_event(event);
        assert!(text.is_none() || text.as_deref() == Some(""));
        assert!(matches!(state, Some(TaskState::Completed)));
    }

    #[test]
    fn parse_sse_event_ignores_unknown_data() {
        let event = "data: {\"not_a2a\": true}";
        let (text, state) = parse_sse_event(event);
        assert!(text.is_none());
        assert!(state.is_none());
    }

    #[tokio::test]
    async fn streaming_mode_accumulates_artifact_text() {
        // Build a minimal SSE response with two artifact events and a COMPLETED status.
        let body_parts = [
            "data: {\"type\":\"TaskStatusUpdateEvent\",\"task\":{\"id\":\"s1\",\"status\":{\"state\":\"TASK_STATE_WORKING\"},\"artifacts\":[]}}\n\n",
            "data: {\"type\":\"TaskArtifactUpdateEvent\",\"task\":{\"id\":\"s1\",\"status\":{\"state\":\"TASK_STATE_WORKING\"},\"artifacts\":[{\"parts\":[{\"text\":\"Hello \"}],\"index\":0}]}}\n\n",
            "data: {\"type\":\"TaskArtifactUpdateEvent\",\"task\":{\"id\":\"s1\",\"status\":{\"state\":\"TASK_STATE_WORKING\"},\"artifacts\":[{\"parts\":[{\"text\":\"world!\"}],\"index\":0}]}}\n\n",
            "data: {\"type\":\"TaskStatusUpdateEvent\",\"task\":{\"id\":\"s1\",\"status\":{\"state\":\"TASK_STATE_COMPLETED\"},\"artifacts\":[]}}\n\n",
        ].concat();
        let sse_body = body_parts.as_str();
        let raw_resp = format!(
            "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
            sse_body.len(),
            sse_body
        );
        let raw_resp: &'static str = Box::leak(raw_resp.into_boxed_str());
        let addr = spawn_mock_server(raw_resp);
        tokio::time::sleep(Duration::from_millis(30)).await;

        let tool = A2aCallTool::new();
        let result = tool
            .execute(json!({
                "url": format!("http://{addr}"),
                "prompt": "say hello",
                "streaming": true,
                "timeout_secs": 10
            }))
            .await
            .unwrap();

        assert_eq!(result, "Hello world!", "got: {result}");
    }

    // ── Goal 179: async_mode + a2a_task_check tests ───────────────────────

    #[tokio::test]
    async fn async_mode_returns_task_id_immediately() {
        let body = r#"{"task":{"id":"async-t1","status":{"state":"TASK_STATE_SUBMITTED"},"artifacts":[]}}"#;
        let raw_resp = Box::leak(make_json_response(body).into_boxed_str());
        let addr = spawn_mock_server(raw_resp);
        tokio::time::sleep(Duration::from_millis(30)).await;

        let tool = A2aCallTool::new();
        let result = tool
            .execute(json!({
                "url": format!("http://{addr}"),
                "prompt": "do something slow",
                "async_mode": true
            }))
            .await
            .unwrap();

        assert!(result.contains("TASK_ID: async-t1"), "got: {result}");
        assert!(result.contains("TASK_STATE_SUBMITTED"), "got: {result}");
        assert!(result.contains("run_background"), "got: {result}");
        assert!(result.contains("curl"), "got: {result}");
    }

    #[tokio::test]
    async fn a2a_task_check_completed_task() {
        let body = r#"{"id":"check-t1","status":{"state":"TASK_STATE_COMPLETED"},"artifacts":[{"parts":[{"text":"Done!"}]}]}"#;
        let raw_resp = Box::leak(make_json_response(body).into_boxed_str());
        let addr = spawn_mock_server(raw_resp);
        tokio::time::sleep(Duration::from_millis(30)).await;

        let tool = A2aTaskCheckTool::new();
        let result = tool
            .execute(json!({
                "url": format!("http://{addr}"),
                "task_id": "check-t1"
            }))
            .await
            .unwrap();

        assert!(result.contains("TASK_STATE_COMPLETED"), "got: {result}");
        assert!(result.contains("Done!"), "got: {result}");
    }

    #[tokio::test]
    async fn a2a_task_check_working_task_hints_to_retry() {
        let body = r#"{"id":"check-t2","status":{"state":"TASK_STATE_WORKING"},"artifacts":[]}"#;
        let raw_resp = Box::leak(make_json_response(body).into_boxed_str());
        let addr = spawn_mock_server(raw_resp);
        tokio::time::sleep(Duration::from_millis(30)).await;

        let tool = A2aTaskCheckTool::new();
        let result = tool
            .execute(json!({
                "url": format!("http://{addr}"),
                "task_id": "check-t2"
            }))
            .await
            .unwrap();

        assert!(result.contains("TASK_STATE_WORKING"), "got: {result}");
        assert!(result.contains("check again later"), "got: {result}");
    }

    #[tokio::test]
    async fn a2a_task_check_http_error_returns_error_string() {
        let raw_resp =
            "HTTP/1.1 404 Not Found\r\nContent-Length: 9\r\nConnection: close\r\n\r\nNot Found";
        let addr = spawn_mock_server(raw_resp);
        tokio::time::sleep(Duration::from_millis(30)).await;

        let tool = A2aTaskCheckTool::new();
        let result = tool
            .execute(json!({
                "url": format!("http://{addr}"),
                "task_id": "missing-task"
            }))
            .await
            .unwrap();

        assert!(result.starts_with("ERROR: HTTP 404"), "got: {result}");
    }
}