awaken-server 0.4.0

Multi-protocol HTTP server with SSE, mailbox, and protocol adapters for Awaken
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
//! A2A loopback integration test — orchestrator delegates to a worker agent
//! on the **same** server via the A2A HTTP protocol.
//!
//! Requires a live OpenAI-compatible LLM. Set these env vars before running:
//!
//! ```bash
//! export OPENAI_BASE_URL=http://localhost:8000/codex/v1
//! export OPENAI_API_KEY=sk-ccproxy
//! export OPENAI_MODEL=gpt-5.4        # optional, defaults to "gpt-4o-mini"
//! export OPENAI_ADAPTER=deepseek      # optional, defaults to "deepseek"
//! ```
//!
//! Run with:
//!   cargo test -p awaken-server --test a2a_loopback -- --ignored

use std::sync::{Arc, Mutex};
use std::time::Duration;

use async_trait::async_trait;
use awaken_contract::contract::content::ContentBlock;
use awaken_contract::contract::executor::{InferenceExecutionError, InferenceRequest, LlmExecutor};
use awaken_contract::contract::inference::{StopReason, StreamResult, TokenUsage};
use awaken_contract::contract::lifecycle::{RunStatus, TerminationReason};
use awaken_contract::contract::message::ToolCall;
use awaken_contract::contract::storage::{RunQuery, RunStore};
use awaken_contract::contract::tool::{
    Tool, ToolCallContext, ToolDescriptor, ToolError, ToolOutput, ToolResult,
};
use awaken_contract::registry_spec::{AgentSpec, RemoteEndpoint};
use awaken_runtime::builder::AgentRuntimeBuilder;
use awaken_runtime::engine::executor::GenaiExecutor;
use awaken_runtime::extensions::background::{
    BackgroundTaskManager, BackgroundTaskPlugin, TaskParentContext,
    TaskResult as BackgroundTaskResult, TaskStatus,
};
use awaken_runtime::registry::traits::ModelBinding;
use awaken_server::app::{AppState, ServerConfig};
use awaken_server::mailbox::{Mailbox, MailboxConfig};
use awaken_server::routes::build_router;
use awaken_stores::InMemoryMailboxStore;
use awaken_stores::memory::InMemoryStore;
use serde_json::Value;
use tokio::net::TcpListener;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Build a genai Client using OPENAI_BASE_URL / OPENAI_API_KEY env vars.
fn build_genai_client() -> genai::Client {
    let base_url = std::env::var("OPENAI_BASE_URL").unwrap_or_default();
    let api_key = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY must be set");

    if base_url.is_empty() {
        // Use default genai client (routes via genai's built-in resolver)
        return genai::Client::default();
    }

    use genai::resolver::{AuthData, Endpoint};
    use genai::{ModelIden, ServiceTarget};

    let mut url = base_url;
    if !url.ends_with('/') {
        url.push('/');
    }

    let adapter_override = std::env::var("OPENAI_ADAPTER").ok();

    genai::Client::builder()
        .with_service_target_resolver_fn(move |st: ServiceTarget| {
            let adapter_kind = resolve_openai_compatible_adapter(
                &st.model.model_name,
                adapter_override.as_deref(),
            );
            Ok(ServiceTarget {
                endpoint: Endpoint::from_owned(url.clone()),
                auth: AuthData::from_single(api_key.clone()),
                model: ModelIden::new(adapter_kind, st.model.model_name),
            })
        })
        .build()
}

fn resolve_openai_compatible_adapter(
    model_name: &str,
    adapter_override: Option<&str>,
) -> genai::adapter::AdapterKind {
    use genai::adapter::AdapterKind;

    match adapter_override
        .map(str::trim)
        .filter(|value| !value.is_empty())
    {
        Some(value) => match value.to_ascii_lowercase().as_str() {
            "openai" => AdapterKind::OpenAI,
            "openai_resp" | "openai-resp" | "responses" => AdapterKind::OpenAIResp,
            "deepseek" => AdapterKind::DeepSeek,
            "together" => AdapterKind::Together,
            "fireworks" => AdapterKind::Fireworks,
            _ => infer_openai_compatible_adapter(model_name),
        },
        None => infer_openai_compatible_adapter(model_name),
    }
}

fn infer_openai_compatible_adapter(model_name: &str) -> genai::adapter::AdapterKind {
    use genai::adapter::AdapterKind;

    let inferred = AdapterKind::from_model(model_name).unwrap_or(AdapterKind::OpenAI);
    match inferred {
        AdapterKind::OpenAI
        | AdapterKind::OpenAIResp
        | AdapterKind::DeepSeek
        | AdapterKind::Together
        | AdapterKind::Fireworks => inferred,
        _ => AdapterKind::OpenAI,
    }
}

/// POST JSON and return parsed body.
async fn post_json(client: &reqwest::Client, url: &str, body: Value) -> Value {
    let resp = client
        .post(url)
        .json(&body)
        .send()
        .await
        .expect("POST request");
    let status = resp.status();
    let text = resp.text().await.expect("response body");
    assert!(status.is_success(), "POST {url} failed ({status}): {text}");
    serde_json::from_str(&text).expect("valid JSON response")
}

/// GET JSON and return parsed body.
async fn get_json(client: &reqwest::Client, url: &str) -> Value {
    let resp = client.get(url).send().await.expect("GET request");
    let status = resp.status();
    let text = resp.text().await.expect("response body");
    assert!(status.is_success(), "GET {url} failed ({status}): {text}");
    serde_json::from_str(&text).expect("valid JSON response")
}

struct ScriptedLlm {
    responses: Mutex<Vec<StreamResult>>,
}

impl ScriptedLlm {
    fn new(responses: Vec<StreamResult>) -> Self {
        Self {
            responses: Mutex::new(responses),
        }
    }

    fn tool_call_response(calls: Vec<ToolCall>) -> StreamResult {
        StreamResult {
            content: vec![],
            tool_calls: calls,
            usage: Some(TokenUsage::default()),
            stop_reason: Some(StopReason::ToolUse),
            has_incomplete_tool_calls: false,
        }
    }

    fn text_response(text: &str) -> StreamResult {
        StreamResult {
            content: vec![ContentBlock::text(text)],
            tool_calls: vec![],
            usage: Some(TokenUsage::default()),
            stop_reason: Some(StopReason::EndTurn),
            has_incomplete_tool_calls: false,
        }
    }
}

#[async_trait]
impl LlmExecutor for ScriptedLlm {
    async fn execute(
        &self,
        _request: InferenceRequest,
    ) -> Result<StreamResult, InferenceExecutionError> {
        let mut responses = self.responses.lock().expect("responses lock");
        Ok(if responses.is_empty() {
            ScriptedLlm::text_response("")
        } else {
            responses.remove(0)
        })
    }

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

struct SpawnBackgroundChildTool {
    manager: Arc<BackgroundTaskManager>,
}

#[async_trait]
impl Tool for SpawnBackgroundChildTool {
    fn descriptor(&self) -> ToolDescriptor {
        ToolDescriptor::new(
            "spawn_bg_child",
            "spawn_bg_child",
            "Spawn a cancellable background child task",
        )
    }

    async fn execute(&self, args: Value, ctx: &ToolCallContext) -> Result<ToolOutput, ToolError> {
        let name = args.get("name").and_then(Value::as_str).unwrap_or("leaf");
        let task_id = self
            .manager
            .spawn(
                &ctx.run_identity.thread_id,
                "background_child",
                Some(name),
                "child spawned from loopback worker",
                TaskParentContext::default(),
                |task_ctx| async move {
                    task_ctx.cancelled().await;
                    BackgroundTaskResult::Cancelled
                },
            )
            .await
            .map_err(|error| ToolError::ExecutionFailed(error.to_string()))?;

        Ok(ToolResult::success("spawn_bg_child", serde_json::json!({ "task_id": task_id })).into())
    }
}

// ---------------------------------------------------------------------------
// Test
// ---------------------------------------------------------------------------

#[tokio::test]
async fn a2a_loopback_remote_self_cancel_cascades_background_children() {
    let listener = TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind to random port");
    let addr = listener.local_addr().expect("local addr");
    let base_url = format!("http://127.0.0.1:{}", addr.port());
    let manager = Arc::new(BackgroundTaskManager::new());

    let orchestrator_executor = Arc::new(ScriptedLlm::new(vec![
        ScriptedLlm::tool_call_response(vec![ToolCall::new(
            "delegate-1",
            "agent_run_worker-remote",
            serde_json::json!({"prompt": "spawn a child and cancel yourself"}),
        )]),
        ScriptedLlm::text_response("observed remote cancellation"),
    ]));
    let worker_executor = Arc::new(ScriptedLlm::new(vec![ScriptedLlm::tool_call_response(
        vec![
            ToolCall::new(
                "spawn-1",
                "spawn_bg_child",
                serde_json::json!({"name": "leaf"}),
            ),
            ToolCall::new(
                "cancel-1",
                "cancel_task",
                serde_json::json!({"target": {"relation": "self"}}),
            ),
        ],
    )]));

    let orchestrator_spec = AgentSpec {
        id: "orchestrator".into(),
        model_id: "orchestrator-model".into(),
        system_prompt: "delegate to the remote worker".into(),
        max_rounds: 2,
        delegates: vec!["worker-remote".into()],
        ..Default::default()
    };
    let worker_local = AgentSpec {
        id: "worker".into(),
        model_id: "worker-model".into(),
        system_prompt: "spawn a child and cancel yourself".into(),
        max_rounds: 2,
        plugin_ids: vec!["background".into()],
        ..Default::default()
    };
    let worker_remote = AgentSpec {
        id: "worker-remote".into(),
        model_id: "worker-model".into(),
        system_prompt: "remote worker".into(),
        endpoint: Some(RemoteEndpoint {
            backend: "a2a".into(),
            base_url: format!("{base_url}/v1/a2a"),
            auth: None,
            target: Some("worker".into()),
            timeout_ms: 5_000,
            options: std::collections::BTreeMap::from([(
                "poll_interval_ms".into(),
                serde_json::json!(10_u64),
            )]),
        }),
        ..Default::default()
    };

    let store = Arc::new(InMemoryStore::new());
    let runtime = Arc::new(
        AgentRuntimeBuilder::new()
            .with_provider("orchestrator", orchestrator_executor)
            .with_provider("worker", worker_executor)
            .with_model_binding(
                "orchestrator-model",
                ModelBinding {
                    provider_id: "orchestrator".into(),
                    upstream_model: "mock-orchestrator".into(),
                },
            )
            .with_model_binding(
                "worker-model",
                ModelBinding {
                    provider_id: "worker".into(),
                    upstream_model: "mock-worker".into(),
                },
            )
            .with_tool(
                "spawn_bg_child",
                Arc::new(SpawnBackgroundChildTool {
                    manager: manager.clone(),
                }),
            )
            .with_plugin(
                "background",
                Arc::new(BackgroundTaskPlugin::new(manager.clone())),
            )
            .with_agent_spec(orchestrator_spec)
            .with_agent_spec(worker_local)
            .with_agent_spec(worker_remote)
            .with_thread_run_store(store.clone())
            .build_unchecked()
            .expect("build runtime"),
    );

    let mailbox_store = Arc::new(InMemoryMailboxStore::new());
    let mailbox = Arc::new(Mailbox::new(
        runtime.clone(),
        mailbox_store,
        store.clone(),
        "loopback-test".to_string(),
        MailboxConfig::default(),
    ));
    let state = AppState::new(
        runtime.clone(),
        mailbox,
        store.clone(),
        runtime.resolver_arc(),
        ServerConfig::default(),
    );

    let router = build_router(&state).with_state(state);
    let server_handle = tokio::spawn(async move {
        axum::serve(listener, router).await.ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    let http = reqwest::Client::new();
    let task_id = format!("loopback-cancel-{}", uuid::Uuid::now_v7());
    let submit_resp = post_json(
        &http,
        &format!("{base_url}/v1/a2a/orchestrator/message:send"),
        serde_json::json!({
            "message": {
                "taskId": task_id,
                "contextId": task_id,
                "messageId": format!("msg-{task_id}"),
                "role": "ROLE_USER",
                "parts": [{"text": "spawn a child and cancel yourself"}]
            }
        }),
    )
    .await;

    assert_eq!(submit_resp["task"]["id"].as_str(), Some(task_id.as_str()));
    assert_eq!(
        submit_resp["task"]["status"]["state"].as_str(),
        Some("TASK_STATE_COMPLETED"),
        "orchestrator task should complete after observing remote cancellation: {submit_resp}"
    );

    let orchestrator_run = store
        .load_run(&task_id)
        .await
        .expect("load orchestrator run")
        .expect("orchestrator run should exist");
    assert_eq!(orchestrator_run.status, RunStatus::Done);

    let deadline = tokio::time::Instant::now() + Duration::from_secs(5);
    let worker_run = loop {
        let page = store
            .list_runs(&RunQuery {
                limit: 20,
                ..RunQuery::default()
            })
            .await
            .expect("list runs");
        let worker_runs = page
            .items
            .into_iter()
            .filter(|run| run.agent_id == "worker")
            .collect::<Vec<_>>();
        if let Some(run) = worker_runs
            .iter()
            .find(|run| run.termination_reason == Some(TerminationReason::Cancelled))
            .cloned()
        {
            break run;
        }
        assert!(
            tokio::time::Instant::now() < deadline,
            "cancelled worker run did not appear before deadline: {worker_runs:?}"
        );
        tokio::time::sleep(Duration::from_millis(25)).await;
    };

    assert_eq!(worker_run.status, RunStatus::Done);
    assert_eq!(
        worker_run.termination_reason,
        Some(TerminationReason::Cancelled)
    );

    let tasks = manager.list(&worker_run.thread_id).await;
    assert_eq!(
        tasks.len(),
        1,
        "expected one background child task: {tasks:?}"
    );
    assert_eq!(tasks[0].status, TaskStatus::Cancelled);
    assert_eq!(tasks[0].task_type, "background_child");
    assert_eq!(
        tasks[0].parent_context.run_id.as_deref(),
        Some(worker_run.run_id.as_str())
    );

    let runs = get_json(&http, &format!("{base_url}/v1/runs")).await;
    let worker_statuses = runs["items"]
        .as_array()
        .expect("runs items")
        .iter()
        .filter(|item| item["agent_id"].as_str() == Some("worker"))
        .filter_map(|item| item["status"].as_str())
        .collect::<Vec<_>>();
    assert!(
        worker_statuses.contains(&"done"),
        "expected worker run to be visible in run listing: {runs}"
    );

    server_handle.abort();
}

/// Dual-server A2A loopback: orchestrator server delegates over HTTP to a
/// separate worker server, where the remote worker self-cancels and cascades
/// cancellation to its background descendants.
#[tokio::test]
async fn a2a_dual_server_remote_self_cancel_cascades_background_children() {
    let worker_listener = TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind worker listener");
    let worker_addr = worker_listener.local_addr().expect("worker local addr");
    let worker_base_url = format!("http://127.0.0.1:{}", worker_addr.port());
    let worker_manager = Arc::new(BackgroundTaskManager::new());
    let worker_store = Arc::new(InMemoryStore::new());
    let worker_runtime = Arc::new(
        AgentRuntimeBuilder::new()
            .with_provider(
                "worker",
                Arc::new(ScriptedLlm::new(vec![ScriptedLlm::tool_call_response(
                    vec![
                        ToolCall::new(
                            "spawn-1",
                            "spawn_bg_child",
                            serde_json::json!({"name": "leaf"}),
                        ),
                        ToolCall::new(
                            "cancel-1",
                            "cancel_task",
                            serde_json::json!({"target": {"relation": "self"}}),
                        ),
                    ],
                )])),
            )
            .with_model_binding(
                "worker-model",
                ModelBinding {
                    provider_id: "worker".into(),
                    upstream_model: "mock-worker".into(),
                },
            )
            .with_tool(
                "spawn_bg_child",
                Arc::new(SpawnBackgroundChildTool {
                    manager: worker_manager.clone(),
                }),
            )
            .with_plugin(
                "background",
                Arc::new(BackgroundTaskPlugin::new(worker_manager.clone())),
            )
            .with_agent_spec(AgentSpec {
                id: "worker".into(),
                model_id: "worker-model".into(),
                system_prompt: "spawn a child and cancel yourself".into(),
                max_rounds: 2,
                plugin_ids: vec!["background".into()],
                ..Default::default()
            })
            .with_thread_run_store(worker_store.clone())
            .build()
            .expect("build worker runtime"),
    );
    let worker_mailbox = Arc::new(Mailbox::new(
        worker_runtime.clone(),
        Arc::new(InMemoryMailboxStore::new()),
        worker_store.clone(),
        "dual-loopback-worker".to_string(),
        MailboxConfig::default(),
    ));
    let worker_state = AppState::new(
        worker_runtime.clone(),
        worker_mailbox,
        worker_store.clone(),
        worker_runtime.resolver_arc(),
        ServerConfig::default(),
    );
    let worker_router = build_router(&worker_state).with_state(worker_state);
    let worker_handle = tokio::spawn(async move {
        axum::serve(worker_listener, worker_router).await.ok();
    });

    let orchestrator_listener = TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind orchestrator listener");
    let orchestrator_addr = orchestrator_listener
        .local_addr()
        .expect("orchestrator local addr");
    let orchestrator_base_url = format!("http://127.0.0.1:{}", orchestrator_addr.port());
    let orchestrator_store = Arc::new(InMemoryStore::new());
    let orchestrator_runtime = Arc::new(
        AgentRuntimeBuilder::new()
            .with_provider(
                "orchestrator",
                Arc::new(ScriptedLlm::new(vec![
                    ScriptedLlm::tool_call_response(vec![ToolCall::new(
                        "delegate-1",
                        "agent_run_worker-remote",
                        serde_json::json!({"prompt": "spawn a child and cancel yourself"}),
                    )]),
                    ScriptedLlm::text_response("observed remote cancellation"),
                ])),
            )
            .with_model_binding(
                "orchestrator-model",
                ModelBinding {
                    provider_id: "orchestrator".into(),
                    upstream_model: "mock-orchestrator".into(),
                },
            )
            .with_model_binding(
                "worker-remote-model",
                ModelBinding {
                    provider_id: "orchestrator".into(),
                    upstream_model: "mock-worker-remote".into(),
                },
            )
            .with_agent_spec(AgentSpec {
                id: "orchestrator".into(),
                model_id: "orchestrator-model".into(),
                system_prompt: "delegate to the remote worker".into(),
                max_rounds: 2,
                delegates: vec!["worker-remote".into()],
                ..Default::default()
            })
            .with_agent_spec(AgentSpec {
                id: "worker-remote".into(),
                model_id: "worker-remote-model".into(),
                system_prompt: "remote worker".into(),
                endpoint: Some(RemoteEndpoint {
                    backend: "a2a".into(),
                    base_url: format!("{worker_base_url}/v1/a2a"),
                    auth: None,
                    target: Some("worker".into()),
                    timeout_ms: 5_000,
                    options: std::collections::BTreeMap::from([(
                        "poll_interval_ms".into(),
                        serde_json::json!(10_u64),
                    )]),
                }),
                ..Default::default()
            })
            .with_thread_run_store(orchestrator_store.clone())
            .build_unchecked()
            .expect("build orchestrator runtime"),
    );
    let orchestrator_mailbox = Arc::new(Mailbox::new(
        orchestrator_runtime.clone(),
        Arc::new(InMemoryMailboxStore::new()),
        orchestrator_store.clone(),
        "dual-loopback-orchestrator".to_string(),
        MailboxConfig::default(),
    ));
    let orchestrator_state = AppState::new(
        orchestrator_runtime.clone(),
        orchestrator_mailbox,
        orchestrator_store.clone(),
        orchestrator_runtime.resolver_arc(),
        ServerConfig::default(),
    );
    let orchestrator_router = build_router(&orchestrator_state).with_state(orchestrator_state);
    let orchestrator_handle = tokio::spawn(async move {
        axum::serve(orchestrator_listener, orchestrator_router)
            .await
            .ok();
    });

    tokio::time::sleep(Duration::from_millis(100)).await;

    let http = reqwest::Client::new();
    let task_id = format!("dual-loopback-cancel-{}", uuid::Uuid::now_v7());
    let submit_resp = post_json(
        &http,
        &format!("{orchestrator_base_url}/v1/a2a/orchestrator/message:send"),
        serde_json::json!({
            "message": {
                "taskId": task_id,
                "contextId": task_id,
                "messageId": format!("msg-{task_id}"),
                "role": "ROLE_USER",
                "parts": [{"text": "spawn a child and cancel yourself"}]
            }
        }),
    )
    .await;

    assert_eq!(submit_resp["task"]["id"].as_str(), Some(task_id.as_str()));

    let deadline = tokio::time::Instant::now() + Duration::from_secs(5);
    let mut task_state = submit_resp["task"]["status"]["state"]
        .as_str()
        .unwrap_or("unknown")
        .to_string();
    while task_state != "TASK_STATE_COMPLETED" {
        assert!(
            tokio::time::Instant::now() < deadline,
            "orchestrator task did not complete before deadline; last state: {task_state}"
        );
        tokio::time::sleep(Duration::from_millis(25)).await;
        let snapshot = get_json(
            &http,
            &format!("{orchestrator_base_url}/v1/a2a/orchestrator/tasks/{task_id}"),
        )
        .await;
        task_state = snapshot["status"]["state"]
            .as_str()
            .unwrap_or("unknown")
            .to_string();
    }

    let orchestrator_run = orchestrator_store
        .load_run(&task_id)
        .await
        .expect("load orchestrator run")
        .expect("orchestrator run should exist");
    assert_eq!(orchestrator_run.status, RunStatus::Done);
    assert_eq!(orchestrator_run.agent_id, "orchestrator");

    let worker_run = loop {
        let page = worker_store
            .list_runs(&RunQuery {
                limit: 20,
                ..RunQuery::default()
            })
            .await
            .expect("list worker runs");
        let worker_runs = page
            .items
            .into_iter()
            .filter(|run| run.agent_id == "worker")
            .collect::<Vec<_>>();
        if let Some(run) = worker_runs
            .iter()
            .find(|run| run.termination_reason == Some(TerminationReason::Cancelled))
            .cloned()
        {
            break run;
        }
        assert!(
            tokio::time::Instant::now() < deadline,
            "cancelled worker run did not appear before deadline: {worker_runs:?}"
        );
        tokio::time::sleep(Duration::from_millis(25)).await;
    };

    assert_eq!(worker_run.status, RunStatus::Done);
    assert_eq!(
        worker_run.termination_reason,
        Some(TerminationReason::Cancelled)
    );

    let worker_tasks = worker_manager.list(&worker_run.thread_id).await;
    assert_eq!(
        worker_tasks.len(),
        1,
        "expected one background child task on worker server: {worker_tasks:?}"
    );
    assert_eq!(worker_tasks[0].status, TaskStatus::Cancelled);
    assert_eq!(worker_tasks[0].task_type, "background_child");
    assert_eq!(
        worker_tasks[0].parent_context.run_id.as_deref(),
        Some(worker_run.run_id.as_str())
    );

    let orchestrator_runs = get_json(&http, &format!("{orchestrator_base_url}/v1/runs")).await;
    let orchestrator_agents = orchestrator_runs["items"]
        .as_array()
        .expect("orchestrator runs items")
        .iter()
        .filter_map(|item| item["agent_id"].as_str())
        .collect::<Vec<_>>();
    assert!(orchestrator_agents.contains(&"orchestrator"));
    assert!(
        !orchestrator_agents.contains(&"worker"),
        "worker run should not be persisted on orchestrator server: {orchestrator_runs}"
    );

    let worker_runs = get_json(&http, &format!("{worker_base_url}/v1/runs")).await;
    let worker_statuses = worker_runs["items"]
        .as_array()
        .expect("worker runs items")
        .iter()
        .filter(|item| item["agent_id"].as_str() == Some("worker"))
        .filter_map(|item| item["status"].as_str())
        .collect::<Vec<_>>();
    assert!(
        worker_statuses.contains(&"done"),
        "expected worker run to be visible on worker server: {worker_runs}"
    );

    orchestrator_handle.abort();
    worker_handle.abort();
}

/// Full A2A loopback: orchestrator → HTTP A2A → worker (same server) → done.
///
/// The orchestrator's system prompt instructs it to ALWAYS delegate via the
/// `agent_run_worker` tool. The worker simply replies with "PONG".
/// We verify the orchestrator completes and the worker thread is created.
#[tokio::test]
#[ignore] // requires OPENAI_API_KEY
async fn a2a_loopback_orchestrator_delegates_to_worker() {
    if std::env::var("OPENAI_API_KEY").is_err() {
        // OPENAI_API_KEY not set — skip
        return;
    }

    let model_name = std::env::var("OPENAI_MODEL").unwrap_or_else(|_| "gpt-4o-mini".to_string());

    // We need to know the server port before building agents (for RemoteEndpoint).
    // Bind early so we can capture the address.
    let listener = TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind to random port");
    let addr = listener.local_addr().expect("local addr");
    let base_url = format!("http://127.0.0.1:{}", addr.port());

    // Register two worker specs with different IDs:
    //   - "worker" (no endpoint) — runs locally when A2A task_send arrives
    //   - "worker-remote" (with endpoint) — orchestrator delegates to it via A2A HTTP
    // This avoids infinite recursion (worker resolving its own endpoint).

    let worker_local = AgentSpec::new("worker")
        .with_model_id("default")
        .with_system_prompt(
            "You are a simple worker agent. \
             Always reply with exactly one word: PONG. \
             Do not use any tools. Do not add explanation.",
        )
        .with_max_rounds(1);

    let worker_remote = AgentSpec {
        id: "worker-remote".into(),
        model_id: "default".into(),
        system_prompt: "Reply with PONG".into(),
        max_rounds: 1,
        endpoint: Some(RemoteEndpoint {
            backend: "a2a".into(),
            base_url: base_url.clone(),
            auth: None,
            target: Some("worker".into()),
            timeout_ms: 60_000,
            options: std::collections::BTreeMap::from([(
                "poll_interval_ms".into(),
                serde_json::json!(500),
            )]),
        }),
        ..Default::default()
    };

    let orchestrator_spec = AgentSpec::new("orchestrator")
        .with_model_id("default")
        .with_system_prompt(
            "You are an orchestrator. You MUST delegate every user request to the worker \
             by calling the `agent_run_worker-remote` tool with the user's message as the prompt. \
             After receiving the worker's response, reply to the user with the worker's answer. \
             ALWAYS call the tool first — never answer directly.",
        )
        .with_max_rounds(3)
        .with_delegate("worker-remote");

    let executor: Arc<dyn awaken_contract::contract::executor::LlmExecutor> =
        Arc::new(GenaiExecutor::with_client(build_genai_client()));

    let store = Arc::new(InMemoryStore::new());
    let runtime = Arc::new(
        AgentRuntimeBuilder::new()
            .with_provider("default", executor)
            .with_model_binding(
                "default",
                ModelBinding {
                    provider_id: "default".into(),
                    upstream_model: model_name,
                },
            )
            .with_agent_spec(orchestrator_spec)
            .with_agent_spec(worker_local)
            .with_agent_spec(worker_remote)
            .with_thread_run_store(store.clone())
            // Use build_unchecked: the "worker-remote" spec has an endpoint
            // and cannot be resolved locally (by design — it's a remote delegate).
            .build_unchecked()
            .expect("build runtime"),
    );

    let mailbox_store = Arc::new(InMemoryMailboxStore::new());
    let mailbox = Arc::new(Mailbox::new(
        runtime.clone(),
        mailbox_store,
        store.clone(),
        "loopback-test".to_string(),
        MailboxConfig::default(),
    ));

    let state = AppState::new(
        runtime.clone(),
        mailbox,
        store.clone(),
        runtime.resolver_arc(),
        ServerConfig::default(),
    );

    // -- Start server ---------------------------------------------------------

    let router = build_router(&state).with_state(state);
    let server_handle = tokio::spawn(async move {
        axum::serve(listener, router).await.ok();
    });

    // Give the server a moment to start
    tokio::time::sleep(Duration::from_millis(100)).await;

    // -- Submit task to orchestrator via A2A -----------------------------------

    let http = reqwest::Client::new();
    let task_id = format!("loopback-{}", uuid::Uuid::now_v7());

    let submit_resp = post_json(
        &http,
        &format!("{base_url}/v1/a2a/orchestrator/message:send"),
        serde_json::json!({
            "message": {
                "taskId": task_id,
                "contextId": task_id,
                "messageId": format!("msg-{task_id}"),
                "role": "ROLE_USER",
                "parts": [{"text": "Say ping to the worker"}]
            },
            "configuration": {
                "returnImmediately": true
            }
        }),
    )
    .await;

    assert_eq!(
        submit_resp["task"]["id"].as_str(),
        Some(task_id.as_str()),
        "task ID preserved"
    );
    assert_eq!(
        submit_resp["task"]["status"]["state"].as_str(),
        Some("TASK_STATE_SUBMITTED"),
        "initial state is submitted"
    );

    // -- Poll until orchestrator completes ------------------------------------

    let deadline = tokio::time::Instant::now() + Duration::from_secs(90);
    let mut final_state = String::new();

    loop {
        tokio::time::sleep(Duration::from_millis(1000)).await;

        if tokio::time::Instant::now() >= deadline {
            panic!("orchestrator did not complete within 90s — last state: {final_state}");
        }

        // Poll via latest thread run (task_id = thread_id)
        let url = format!("{base_url}/v1/threads/{task_id}/runs/latest");
        let resp = http.get(&url).send().await.expect("poll request");

        if resp.status() == reqwest::StatusCode::NOT_FOUND {
            // Run not created yet, keep waiting
            continue;
        }

        let body: Value = resp.json().await.expect("poll JSON");
        let status = body["status"].as_str().unwrap_or("unknown").to_string();

        tracing::info!("[loopback] orchestrator status: {status}");
        final_state = status.clone();

        if status == "done" {
            break;
        }
    }

    assert_eq!(final_state, "done", "orchestrator should complete");

    // -- Verify worker was invoked (its thread exists) ------------------------

    // Verify ALL runs — the A2A backend submitted a task for the worker,
    // creating a separate thread/run on the same server.
    let runs_resp = get_json(&http, &format!("{base_url}/v1/runs")).await;
    let items = runs_resp["items"].as_array().expect("runs items array");

    let agent_ids: Vec<&str> = items
        .iter()
        .filter_map(|r| r["agent_id"].as_str())
        .collect();

    tracing::info!("[loopback] all runs agents: {agent_ids:?}");

    assert!(
        agent_ids.contains(&"orchestrator"),
        "orchestrator run should exist: {agent_ids:?}"
    );

    // The worker run should exist (A2aBackend now sends agentId="worker-remote",
    // which maps to the local "worker" agent on the server side via A2A routing).
    // The server resolves the agentId from the A2A request; if unknown, falls
    // back to default. Either way, we expect at least 2 runs.
    assert!(
        items.len() >= 2,
        "expected at least 2 runs (orchestrator + worker), got {}: {agent_ids:?}",
        items.len()
    );

    // loopback verified: orchestrator delegated to worker via HTTP

    server_handle.abort();
}