jamjet-worker 0.3.2

JamJet worker process — task execution, heartbeat, lease renewal
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
//! Executor for `AgentTool` workflow nodes (Phase B — sync, streaming, and conversational modes).
//!
//! Invokes a remote agent via A2A-style HTTP POST and returns the response as the node output.
//! - Sync:           POST `{agent_uri}/tasks/send`           — single request/response
//! - Streaming:      POST `{agent_uri}/tasks/sendSubscribe`  — NDJSON stream with budget guard
//! - Conversational: POST `{agent_uri}/tasks/send` in a loop — multi-turn exchange

#![allow(clippy::too_many_arguments)]

use crate::executor::{ExecutionResult, NodeExecutor, StreamEventSender};
use async_trait::async_trait;
use bytes::BytesMut;
use futures::StreamExt;
use jamjet_state::backend::WorkItem;
use reqwest::Client;
use serde_json::{json, Value};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::time::Duration;
use tracing::{debug, instrument, warn};

pub struct AgentToolExecutor;

impl AgentToolExecutor {
    /// Build an HTTP client with the given timeout.
    fn build_client(timeout_ms: u64) -> Result<Client, String> {
        reqwest::Client::builder()
            .timeout(std::time::Duration::from_millis(timeout_ms))
            .build()
            .map_err(|e| format!("HTTP client: {e}"))
    }

    /// Resolve the base agent URI to an `https://` URL, returning an error for unsupported schemes.
    /// In test builds, `http://` is also accepted (for wiremock).
    fn resolve_url(agent_uri: &str, endpoint: &str) -> Result<String, String> {
        let is_http =
            agent_uri.starts_with("https://") || (cfg!(test) && agent_uri.starts_with("http://"));
        if is_http {
            Ok(format!("{}/{}", agent_uri.trim_end_matches('/'), endpoint))
        } else {
            Err(format!(
                "Cannot resolve '{}' to HTTP endpoint. \
                 Only https:// agent URIs are supported for remote invocation.",
                agent_uri
            ))
        }
    }

    /// Execute in sync mode: single POST to `/tasks/send`, returns one response.
    async fn execute_sync(
        &self,
        item: &WorkItem,
        agent_uri: &str,
        protocol: &str,
        output_key: &str,
        timeout_ms: u64,
        input: &Value,
        input_hash: &str,
        start: std::time::Instant,
    ) -> Result<ExecutionResult, String> {
        let client = Self::build_client(timeout_ms)?;
        let task_url = Self::resolve_url(agent_uri, "tasks/send")?;

        let resp = client
            .post(&task_url)
            .json(&json!({
                "jsonrpc": "2.0",
                "method": "tasks/send",
                "params": { "message": { "parts": [{ "text": input.to_string() }] } }
            }))
            .send()
            .await
            .map_err(|e| format!("AgentTool invocation failed: {e}"))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(format!("Agent returned error {status}: {body}"));
        }

        let output: Value = resp
            .json()
            .await
            .map_err(|e| format!("Failed to parse agent response: {e}"))?;

        let duration_ms = start.elapsed().as_millis() as u64;

        Ok(ExecutionResult {
            output: json!({ output_key: output }),
            state_patch: json!({
                "agent_tool_events": [
                    {
                        "type": "agent_tool_invoked",
                        "node_id": &item.node_id,
                        "agent_uri": agent_uri,
                        "mode": "sync",
                        "protocol": protocol,
                        "input_hash": input_hash
                    },
                    {
                        "type": "agent_tool_completed",
                        "node_id": &item.node_id,
                        "output": &output,
                        "total_cost": 0.0,
                        "latency_ms": duration_ms
                    },
                ]
            }),
            duration_ms,
            gen_ai_system: None,
            gen_ai_model: None,
            input_tokens: None,
            output_tokens: None,
            finish_reason: None,
        })
    }

    /// Legacy streaming mode: POST to `/tasks/sendSubscribe`, buffer full body, parse NDJSON.
    ///
    /// Emits `agent_tool_progress` events per chunk, with optional early termination
    /// when `max_cost_usd` budget is exceeded. Final event is `agent_tool_completed`
    /// or `agent_tool_terminated` on budget breach.
    ///
    /// Kept as fallback when invoked through `execute()` (no channel available).
    /// The incremental path is `execute_streaming` on the trait.
    async fn stream_ndjson(
        &self,
        item: &WorkItem,
        agent_uri: &str,
        protocol: &str,
        output_key: &str,
        timeout_ms: u64,
        input: &Value,
        input_hash: &str,
        max_cost_usd: Option<f64>,
        start: std::time::Instant,
    ) -> Result<ExecutionResult, String> {
        let client = Self::build_client(timeout_ms)?;
        let task_url = Self::resolve_url(agent_uri, "tasks/sendSubscribe")?;

        let resp = client
            .post(&task_url)
            .json(&json!({
                "jsonrpc": "2.0",
                "method": "tasks/sendSubscribe",
                "params": { "message": { "parts": [{ "text": input.to_string() }] } }
            }))
            .send()
            .await
            .map_err(|e| format!("AgentTool streaming invocation failed: {e}"))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(format!("Agent returned error {status}: {body}"));
        }

        // Read full body, then parse NDJSON line by line
        let body = resp
            .text()
            .await
            .map_err(|e| format!("Failed to read streaming response body: {e}"))?;

        let mut events: Vec<Value> = vec![json!({
            "type": "agent_tool_invoked",
            "node_id": &item.node_id,
            "agent_uri": agent_uri,
            "mode": "streaming",
            "protocol": protocol,
            "input_hash": input_hash
        })];

        let mut accumulated_cost: f64 = 0.0;
        let mut terminated_early = false;
        let mut last_chunk: Value = json!(null);
        let mut chunk_index: u64 = 0;

        for line in body.lines() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }

            let chunk: Value =
                serde_json::from_str(trimmed).unwrap_or_else(|_| json!({ "raw": trimmed }));

            // Extract cost from chunk if present
            if let Some(cost) = chunk.get("cost_usd").and_then(|v| v.as_f64()) {
                accumulated_cost += cost;
            }

            events.push(json!({
                "type": "agent_tool_progress",
                "node_id": &item.node_id,
                "chunk_index": chunk_index,
                "chunk": &chunk,
                "accumulated_cost_usd": accumulated_cost
            }));

            last_chunk = chunk;
            chunk_index += 1;

            // Budget guard — terminate early if cost threshold exceeded
            if let Some(budget) = max_cost_usd {
                if accumulated_cost > budget {
                    terminated_early = true;
                    debug!(
                        node_id = %item.node_id,
                        accumulated_cost,
                        budget,
                        "AgentTool streaming: budget exceeded, terminating early"
                    );
                    break;
                }
            }
        }

        let duration_ms = start.elapsed().as_millis() as u64;

        if terminated_early {
            events.push(json!({
                "type": "agent_tool_terminated",
                "node_id": &item.node_id,
                "reason": "budget_exceeded",
                "accumulated_cost_usd": accumulated_cost,
                "latency_ms": duration_ms
            }));
        } else {
            events.push(json!({
                "type": "agent_tool_completed",
                "node_id": &item.node_id,
                "output": &last_chunk,
                "total_cost": accumulated_cost,
                "latency_ms": duration_ms
            }));
        }

        Ok(ExecutionResult {
            output: json!({ output_key: &last_chunk }),
            state_patch: json!({ "agent_tool_events": events }),
            duration_ms,
            gen_ai_system: None,
            gen_ai_model: None,
            input_tokens: None,
            output_tokens: None,
            finish_reason: None,
        })
    }

    /// Best-effort A2A cancel. Fire-and-forget with 5s timeout.
    async fn send_a2a_cancel(client: &Client, agent_uri: &str, task_id: &Option<String>) {
        if let Some(ref id) = task_id {
            if let Ok(cancel_url) = Self::resolve_url(agent_uri, "tasks/cancel") {
                let _ = client
                    .post(&cancel_url)
                    .json(&serde_json::json!({ "id": id }))
                    .timeout(Duration::from_secs(5))
                    .send()
                    .await;
            }
        }
    }

    /// Execute in conversational mode: multi-turn loop sending to `/tasks/send`.
    ///
    /// Reads `max_turns` from `payload.mode.conversational.max_turns` (default 5).
    /// Each turn records outbound and inbound `agent_tool_turn` events. Stops early
    /// when the agent response carries `status: "completed"`.
    async fn execute_conversational(
        &self,
        item: &WorkItem,
        agent_uri: &str,
        protocol: &str,
        output_key: &str,
        timeout_ms: u64,
        input: &Value,
        input_hash: &str,
        start: std::time::Instant,
    ) -> Result<ExecutionResult, String> {
        let p = &item.payload;

        let max_turns = p
            .get("mode")
            .and_then(|m| m.get("conversational"))
            .and_then(|c| c.get("max_turns"))
            .and_then(|v| v.as_u64())
            .unwrap_or(5) as usize;

        let client = Self::build_client(timeout_ms)?;
        let task_url = Self::resolve_url(agent_uri, "tasks/send")?;

        let mut events: Vec<Value> = vec![json!({
            "type": "agent_tool_invoked",
            "node_id": &item.node_id,
            "agent_uri": agent_uri,
            "mode": "conversational",
            "protocol": protocol,
            "input_hash": input_hash
        })];

        let mut current_input = input.clone();
        let mut final_output: Value = json!(null);

        for turn in 0..max_turns {
            // Record outbound turn event
            events.push(json!({
                "type": "agent_tool_turn",
                "node_id": &item.node_id,
                "turn": turn,
                "direction": "outbound",
                "input": &current_input
            }));

            debug!(
                node_id = %item.node_id,
                turn,
                "AgentTool conversational: sending turn"
            );

            let resp = client
                .post(&task_url)
                .json(&json!({
                    "jsonrpc": "2.0",
                    "method": "tasks/send",
                    "params": { "message": { "parts": [{ "text": current_input.to_string() }] } }
                }))
                .send()
                .await
                .map_err(|e| format!("AgentTool turn {turn} failed: {e}"))?;

            if !resp.status().is_success() {
                let status = resp.status();
                let body = resp.text().await.unwrap_or_default();
                return Err(format!(
                    "Agent returned error {status} on turn {turn}: {body}"
                ));
            }

            let response: Value = resp
                .json()
                .await
                .map_err(|e| format!("Failed to parse agent response on turn {turn}: {e}"))?;

            // Record inbound turn event
            events.push(json!({
                "type": "agent_tool_turn",
                "node_id": &item.node_id,
                "turn": turn,
                "direction": "inbound",
                "output": &response
            }));

            final_output = response.clone();

            // Check if the agent signals completion
            let status = response
                .get("status")
                .and_then(|v| v.as_str())
                .unwrap_or("");
            if status == "completed" {
                debug!(
                    node_id = %item.node_id,
                    turn,
                    "AgentTool conversational: agent signalled completion"
                );
                break;
            }

            // Use agent output as next input for the following turn
            current_input = response
                .get("output")
                .cloned()
                .unwrap_or_else(|| response.clone());
        }

        let duration_ms = start.elapsed().as_millis() as u64;

        events.push(json!({
            "type": "agent_tool_completed",
            "node_id": &item.node_id,
            "output": &final_output,
            "total_cost": 0.0,
            "latency_ms": duration_ms
        }));

        Ok(ExecutionResult {
            output: json!({ output_key: &final_output }),
            state_patch: json!({ "agent_tool_events": events }),
            duration_ms,
            gen_ai_system: None,
            gen_ai_model: None,
            input_tokens: None,
            output_tokens: None,
            finish_reason: None,
        })
    }
}

#[async_trait]
impl NodeExecutor for AgentToolExecutor {
    #[instrument(skip(self, item), fields(node_id = %item.node_id))]
    async fn execute(&self, item: &WorkItem) -> Result<ExecutionResult, String> {
        let start = std::time::Instant::now();
        let p = &item.payload;

        // Extract agent target — handle both { "explicit": "uri" } and plain string
        let agent_uri = p
            .get("agent")
            .and_then(|a| {
                a.get("explicit")
                    .and_then(|v| v.as_str())
                    .or_else(|| a.as_str())
            })
            .ok_or("AgentTool: missing 'agent' URI in payload")?;

        // Extract mode — handle both string and object forms
        // e.g. "sync" or {"conversational": {"max_turns": 5}}
        let mode = if let Some(mode_val) = p.get("mode") {
            if let Some(s) = mode_val.as_str() {
                s.to_string()
            } else if mode_val.get("conversational").is_some() {
                "conversational".to_string()
            } else if mode_val.get("streaming").is_some() {
                "streaming".to_string()
            } else {
                "sync".to_string()
            }
        } else {
            "sync".to_string()
        };
        let output_key = p
            .get("output_key")
            .and_then(|v| v.as_str())
            .unwrap_or("result");
        let timeout_ms = p
            .get("timeout_ms")
            .and_then(|v| v.as_u64())
            .unwrap_or(30_000);
        let input = p.get("input").cloned().unwrap_or(json!({}));
        // Budget lookup: check nested {"budget": {"max_cost_usd": …}} first, then flat "max_cost_usd"
        let max_cost_usd = p
            .get("budget")
            .and_then(|b| b.get("max_cost_usd"))
            .and_then(|v| v.as_f64())
            .or_else(|| p.get("max_cost_usd").and_then(|v| v.as_f64()));

        // Check for unresolved auto target
        if p.get("agent").and_then(|a| a.get("auto")).is_some() {
            return Err(
                "AgentTool with 'auto' target was not expanded at compile time. \
                 Use the compiler to expand 'auto' into coordinator + agent_tool nodes."
                    .into(),
            );
        }

        // Resolve protocol based on URI scheme
        let protocol = if agent_uri.starts_with("https://") {
            "a2a"
        } else if agent_uri.starts_with("jamjet://") {
            "local"
        } else {
            "mcp"
        };

        // Compute input hash for tracing
        let mut hasher = DefaultHasher::new();
        input.to_string().hash(&mut hasher);
        let input_hash = format!("{:016x}", hasher.finish());

        debug!(agent_uri = %agent_uri, mode = %mode, protocol = %protocol, "AgentTool: invoking");

        match mode.as_str() {
            "sync" => {
                self.execute_sync(
                    item,
                    agent_uri,
                    protocol,
                    output_key,
                    timeout_ms,
                    &input,
                    &input_hash,
                    start,
                )
                .await
            }
            "streaming" => {
                self.stream_ndjson(
                    item,
                    agent_uri,
                    protocol,
                    output_key,
                    timeout_ms,
                    &input,
                    &input_hash,
                    max_cost_usd,
                    start,
                )
                .await
            }
            "conversational" => {
                self.execute_conversational(
                    item,
                    agent_uri,
                    protocol,
                    output_key,
                    timeout_ms,
                    &input,
                    &input_hash,
                    start,
                )
                .await
            }
            other => Err(format!("Unknown agent_tool mode: '{other}'")),
        }
    }

    /// Incremental NDJSON streaming with per-chunk idle timeout, budget guard,
    /// and A2A cancel on early termination. Events are sent via `tx` in real time.
    #[instrument(skip(self, item, tx), fields(node_id = %item.node_id))]
    async fn execute_streaming(
        &self,
        item: &WorkItem,
        tx: StreamEventSender,
    ) -> Result<ExecutionResult, String> {
        let start = std::time::Instant::now();
        let p = &item.payload;

        // ── Extract params ──────────────────────────────────────────────
        let agent_uri = p
            .get("agent")
            .and_then(|a| {
                a.get("explicit")
                    .and_then(|v| v.as_str())
                    .or_else(|| a.as_str())
            })
            .ok_or("AgentTool: missing 'agent' URI in payload")?;

        let mode = if let Some(mode_val) = p.get("mode") {
            if let Some(s) = mode_val.as_str() {
                s.to_string()
            } else if mode_val.get("streaming").is_some() {
                "streaming".to_string()
            } else {
                "sync".to_string()
            }
        } else {
            "sync".to_string()
        };

        // Short-circuit non-streaming modes back to execute()
        if mode != "streaming" {
            return self.execute(item).await;
        }

        let input = p.get("input").cloned().unwrap_or(json!({}));

        let max_cost_usd = p
            .get("budget")
            .and_then(|b| b.get("max_cost_usd"))
            .and_then(|v| v.as_f64())
            .or_else(|| p.get("max_cost_usd").and_then(|v| v.as_f64()));

        let idle_timeout_secs = p
            .get("idle_timeout_secs")
            .and_then(|v| v.as_u64())
            .unwrap_or(30);

        // Resolve protocol
        let protocol = if agent_uri.starts_with("https://")
            || (cfg!(test) && agent_uri.starts_with("http://"))
        {
            "a2a"
        } else if agent_uri.starts_with("jamjet://") {
            "local"
        } else {
            "mcp"
        };

        // Compute input hash
        let mut hasher = DefaultHasher::new();
        input.to_string().hash(&mut hasher);
        let input_hash = format!("{:016x}", hasher.finish());

        // ── Build client WITHOUT overall timeout (streaming uses per-chunk idle) ──
        let client = reqwest::Client::builder()
            .build()
            .map_err(|e| format!("HTTP client: {e}"))?;

        // ── Emit invoked event ──────────────────────────────────────────
        let now_ms = || -> u64 {
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_millis() as u64
        };

        let invoked_event = json!({
            "type": "agent_tool_invoked",
            "node_id": &item.node_id,
            "agent_uri": agent_uri,
            "mode": &mode,
            "protocol": protocol,
            "input_hash": &input_hash,
            "timestamp_ms": now_ms()
        });
        if tx.send(invoked_event).await.is_err() {
            return Err("Streaming receiver dropped before invocation event".into());
        }

        // ── POST to /tasks/sendSubscribe ────────────────────────────────
        let task_url = Self::resolve_url(agent_uri, "tasks/sendSubscribe")?;
        let resp = client
            .post(&task_url)
            .json(&json!({
                "jsonrpc": "2.0",
                "method": "tasks/sendSubscribe",
                "params": { "message": { "parts": [{ "text": input.to_string() }] } }
            }))
            .send()
            .await
            .map_err(|e| format!("AgentTool streaming invocation failed: {e}"))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(format!("Agent returned error {status}: {body}"));
        }

        // ── Incremental NDJSON read loop ────────────────────────────────
        let mut stream = resp.bytes_stream();
        let mut line_buf = BytesMut::new();
        let mut chunk_index: u64 = 0;
        let mut accumulated_cost: f64 = 0.0;
        let mut task_id: Option<String> = None;
        let mut last_chunk: Value = json!(null);
        let output_key = p
            .get("output_key")
            .and_then(|v| v.as_str())
            .unwrap_or("result");
        let mut terminated_early = false;
        let mut terminal_error: Option<String> = None;
        let idle_dur = Duration::from_secs(idle_timeout_secs);

        loop {
            match tokio::time::timeout(idle_dur, stream.next()).await {
                // Timeout — no data within idle window
                Err(_elapsed) => {
                    warn!(
                        node_id = %item.node_id,
                        idle_timeout_secs,
                        "AgentTool streaming: idle timeout, terminating"
                    );
                    let _ = tx
                        .send(json!({
                            "type": "agent_tool_terminated",
                            "node_id": &item.node_id,
                            "reason": "idle_timeout",
                            "accumulated_cost_usd": accumulated_cost,
                            "latency_ms": start.elapsed().as_millis() as u64,
                            "timestamp_ms": now_ms()
                        }))
                        .await;
                    Self::send_a2a_cancel(&client, agent_uri, &task_id).await;
                    terminated_early = true;
                    terminal_error =
                        Some(format!("AgentTool idle timeout after {idle_timeout_secs}s"));
                    break;
                }
                // Stream ended normally
                Ok(None) => {
                    break;
                }
                // Network error
                Ok(Some(Err(e))) => {
                    warn!(
                        node_id = %item.node_id,
                        error = %e,
                        "AgentTool streaming: network error"
                    );
                    let _ = tx
                        .send(json!({
                            "type": "agent_tool_error",
                            "node_id": &item.node_id,
                            "error": e.to_string(),
                            "timestamp_ms": now_ms()
                        }))
                        .await;
                    terminated_early = true;
                    terminal_error = Some(format!("AgentTool stream error: {e}"));
                    break;
                }
                // Got a chunk of bytes
                Ok(Some(Ok(bytes))) => {
                    line_buf.extend_from_slice(&bytes);

                    // Process all complete lines in the buffer
                    while let Some(newline_pos) = line_buf.iter().position(|&b| b == b'\n') {
                        let line_bytes = line_buf.split_to(newline_pos + 1);
                        let line_str = match std::str::from_utf8(&line_bytes) {
                            Ok(s) => s.trim().to_string(),
                            Err(e) => {
                                warn!(
                                    node_id = %item.node_id,
                                    error = %e,
                                    "AgentTool streaming: non-UTF8 chunk, skipping"
                                );
                                continue;
                            }
                        };
                        if line_str.is_empty() {
                            continue;
                        }

                        let chunk: Value = serde_json::from_str(&line_str)
                            .unwrap_or_else(|_| json!({ "raw": &line_str }));
                        last_chunk = chunk.clone();

                        // Extract task_id from first chunk
                        if task_id.is_none() {
                            if let Some(id) = chunk.get("id").and_then(|v| v.as_str()) {
                                task_id = Some(id.to_string());
                            }
                        }

                        // Accumulate cost
                        if let Some(cost) = chunk.get("cost_usd").and_then(|v| v.as_f64()) {
                            accumulated_cost += cost;
                        }

                        // Emit progress event
                        let progress = json!({
                            "type": "agent_tool_progress",
                            "node_id": &item.node_id,
                            "chunk_index": chunk_index,
                            "chunk": &chunk,
                            "accumulated_cost_usd": accumulated_cost,
                            "timestamp_ms": now_ms()
                        });
                        chunk_index += 1;

                        if tx.send(progress).await.is_err() {
                            // Receiver dropped — treat as cancellation
                            debug!(
                                node_id = %item.node_id,
                                "AgentTool streaming: receiver dropped, cancelling"
                            );
                            Self::send_a2a_cancel(&client, agent_uri, &task_id).await;
                            terminated_early = true;
                            terminal_error = Some("AgentTool stream receiver dropped".into());
                            break;
                        }

                        // Budget guard
                        if let Some(budget) = max_cost_usd {
                            if accumulated_cost > budget {
                                debug!(
                                    node_id = %item.node_id,
                                    accumulated_cost,
                                    budget,
                                    "AgentTool streaming: budget exceeded, terminating"
                                );
                                let _ = tx
                                    .send(json!({
                                        "type": "agent_tool_terminated",
                                        "node_id": &item.node_id,
                                        "reason": "budget_exceeded",
                                        "accumulated_cost_usd": accumulated_cost,
                                        "latency_ms": start.elapsed().as_millis() as u64,
                                        "timestamp_ms": now_ms()
                                    }))
                                    .await;
                                Self::send_a2a_cancel(&client, agent_uri, &task_id).await;
                                terminated_early = true;
                                break;
                            }
                        }
                    }

                    // If inner loop broke due to termination, break outer loop too
                    if terminated_early {
                        break;
                    }
                }
            }
        }

        // ── Drain remaining bytes in line_buf ───────────────────────────
        if !terminated_early && !line_buf.is_empty() {
            if let Ok(remaining) = std::str::from_utf8(&line_buf) {
                let trimmed = remaining.trim();
                if !trimmed.is_empty() {
                    let chunk: Value =
                        serde_json::from_str(trimmed).unwrap_or_else(|_| json!({ "raw": trimmed }));
                    last_chunk = chunk.clone();

                    if let Some(cost) = chunk.get("cost_usd").and_then(|v| v.as_f64()) {
                        accumulated_cost += cost;
                    }

                    let _ = tx
                        .send(json!({
                            "type": "agent_tool_progress",
                            "node_id": &item.node_id,
                            "chunk_index": chunk_index,
                            "chunk": &chunk,
                            "accumulated_cost_usd": accumulated_cost,
                            "timestamp_ms": now_ms()
                        }))
                        .await;
                }
            }
        }

        // ── Return error for hard failures ───────────────────────────────
        if let Some(error) = terminal_error {
            return Err(error);
        }

        // ── Emit completed (if not terminated early) ────────────────────
        let duration_ms = start.elapsed().as_millis() as u64;
        if !terminated_early {
            let _ = tx
                .send(json!({
                    "type": "agent_tool_completed",
                    "node_id": &item.node_id,
                    "output": &last_chunk,
                    "total_cost": accumulated_cost,
                    "latency_ms": duration_ms,
                    "timestamp_ms": now_ms()
                }))
                .await;
        }

        Ok(ExecutionResult {
            output: json!({ output_key: last_chunk }),
            state_patch: json!({}),
            duration_ms,
            gen_ai_system: None,
            gen_ai_model: None,
            input_tokens: None,
            output_tokens: None,
            finish_reason: None,
        })
    }
}

// ── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::executor::NodeExecutor;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    /// Build a WorkItem that targets the given agent URI with streaming mode.
    fn make_test_work_item(
        agent_uri: &str,
        idle_timeout: Option<u64>,
        max_cost: Option<f64>,
    ) -> WorkItem {
        let mut payload = serde_json::json!({
            "agent": agent_uri,
            "mode": "streaming",
            "input": {"query": "test"},
            "workflow_id": "wf1",
            "workflow_version": "1.0.0",
        });
        if let Some(t) = idle_timeout {
            payload["idle_timeout_secs"] = serde_json::json!(t);
        }
        if let Some(c) = max_cost {
            payload["budget"] = serde_json::json!({"max_cost_usd": c});
        }
        WorkItem {
            id: uuid::Uuid::new_v4(),
            execution_id: jamjet_core::workflow::ExecutionId::new(),
            node_id: "n1".into(),
            queue_type: "agent_tool".into(),
            payload,
            attempt: 1,
            max_attempts: 3,
            created_at: chrono::Utc::now(),
            lease_expires_at: None,
            worker_id: None,
            tenant_id: "default".into(),
        }
    }

    /// Join NDJSON lines into a single body terminated by a newline.
    fn ndjson_body(lines: &[&str]) -> String {
        lines.join("\n") + "\n"
    }

    /// Drain all events from the receiver (non-blocking).
    fn collect_events(
        rx: &mut tokio::sync::mpsc::Receiver<serde_json::Value>,
    ) -> Vec<serde_json::Value> {
        let mut events = Vec::new();
        while let Ok(ev) = rx.try_recv() {
            events.push(ev);
        }
        events
    }

    // ── Test 1: streams NDJSON chunks in order ──────────────────────────

    #[tokio::test]
    async fn streams_ndjson_chunks_in_order() {
        let server = MockServer::start().await;

        let body = ndjson_body(&[r#"{"text":"hello"}"#, r#"{"text":"world"}"#]);

        Mock::given(method("POST"))
            .and(path("/tasks/sendSubscribe"))
            .respond_with(ResponseTemplate::new(200).set_body_string(body))
            .mount(&server)
            .await;

        let item = make_test_work_item(&server.uri(), Some(5), None);
        let (tx, mut rx) = tokio::sync::mpsc::channel(32);

        let executor = AgentToolExecutor;
        let result = executor.execute_streaming(&item, tx).await;
        assert!(
            result.is_ok(),
            "execute_streaming failed: {:?}",
            result.err()
        );

        let events = collect_events(&mut rx);

        // Expect: invoked, progress(0), progress(1), completed
        assert!(
            events.len() >= 4,
            "Expected at least 4 events, got {}: {:#?}",
            events.len(),
            events
        );

        assert_eq!(events[0]["type"], "agent_tool_invoked");
        assert_eq!(events[0]["mode"], "streaming");

        assert_eq!(events[1]["type"], "agent_tool_progress");
        assert_eq!(events[1]["chunk_index"], 0);
        assert_eq!(events[1]["chunk"]["text"], "hello");

        assert_eq!(events[2]["type"], "agent_tool_progress");
        assert_eq!(events[2]["chunk_index"], 1);
        assert_eq!(events[2]["chunk"]["text"], "world");

        assert_eq!(events[3]["type"], "agent_tool_completed");
    }

    // ── Test 2: budget exceeded terminates stream ───────────────────────

    #[tokio::test]
    async fn budget_exceeded_terminates_stream() {
        let server = MockServer::start().await;

        // 3 chunks each costing 0.3; budget is 0.5 → should terminate after chunk 1
        let body = ndjson_body(&[
            r#"{"text":"a","cost_usd":0.3}"#,
            r#"{"text":"b","cost_usd":0.3}"#,
            r#"{"text":"c","cost_usd":0.3}"#,
        ]);

        Mock::given(method("POST"))
            .and(path("/tasks/sendSubscribe"))
            .respond_with(ResponseTemplate::new(200).set_body_string(body))
            .mount(&server)
            .await;

        // Also mock /tasks/cancel so the A2A cancel doesn't fail
        Mock::given(method("POST"))
            .and(path("/tasks/cancel"))
            .respond_with(ResponseTemplate::new(200))
            .mount(&server)
            .await;

        let item = make_test_work_item(&server.uri(), Some(5), Some(0.5));
        let (tx, mut rx) = tokio::sync::mpsc::channel(32);

        let executor = AgentToolExecutor;
        let result = executor.execute_streaming(&item, tx).await;
        assert!(result.is_ok());

        let events = collect_events(&mut rx);

        // Find a terminated event with reason "budget_exceeded"
        let terminated = events.iter().find(|e| e["type"] == "agent_tool_terminated");
        assert!(
            terminated.is_some(),
            "Expected an agent_tool_terminated event, got: {:#?}",
            events
        );
        assert_eq!(terminated.unwrap()["reason"], "budget_exceeded");

        // Should NOT have a completed event
        let completed = events.iter().any(|e| e["type"] == "agent_tool_completed");
        assert!(
            !completed,
            "Should not have agent_tool_completed when budget exceeded"
        );
    }

    // ── Test 3: malformed JSON becomes raw ──────────────────────────────

    #[tokio::test]
    async fn malformed_json_becomes_raw() {
        let server = MockServer::start().await;

        let body = ndjson_body(&[
            r#"{"text":"first"}"#,
            "not json at all",
            r#"{"text":"third"}"#,
        ]);

        Mock::given(method("POST"))
            .and(path("/tasks/sendSubscribe"))
            .respond_with(ResponseTemplate::new(200).set_body_string(body))
            .mount(&server)
            .await;

        let item = make_test_work_item(&server.uri(), Some(5), None);
        let (tx, mut rx) = tokio::sync::mpsc::channel(32);

        let executor = AgentToolExecutor;
        let result = executor.execute_streaming(&item, tx).await;
        assert!(result.is_ok());

        let events = collect_events(&mut rx);

        // events[0] = invoked, events[1] = progress(0), events[2] = progress(1), events[3] = progress(2), events[4] = completed
        let progress_events: Vec<&serde_json::Value> = events
            .iter()
            .filter(|e| e["type"] == "agent_tool_progress")
            .collect();

        assert_eq!(
            progress_events.len(),
            3,
            "Expected 3 progress events, got {}: {:#?}",
            progress_events.len(),
            progress_events
        );

        // First chunk: valid JSON
        assert_eq!(progress_events[0]["chunk"]["text"], "first");

        // Second chunk: malformed → wrapped in {"raw": "not json at all"}
        assert_eq!(progress_events[1]["chunk"]["raw"], "not json at all");

        // Third chunk: valid JSON
        assert_eq!(progress_events[2]["chunk"]["text"], "third");
    }
}