everruns-platform 0.34.2

Backend control-plane entities and store contracts for the Everruns Platform
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
//! AG-UI delegation against a local mock AG-UI agent (axum, SSE). Mirrors the
//! A2A delegation tests: foreground and background runs, the generic task
//! tools, egress and secret handling.

use super::run::{AgUiRunStatus, load_run, resume_from_answer};
use super::*;
use crate::capabilities::session_tasks::tests::InMemorySessionTaskRegistry;
use crate::capabilities::session_tasks::{CancelTaskTool, MessageTaskTool, WaitTaskTool};
use axum::Router;
use axum::body::Body;
use axum::extract::State;
use axum::http::{HeaderMap, StatusCode};
use axum::response::Response;
use axum::routing::post;
use everruns_ag_ui::{Interrupt, ResumeStatus};
use everruns_core::network_access::NetworkAccessList;
use everruns_core::session_services::SessionStorageStore;
use everruns_core::session_task::{SessionTaskRegistry, TaskExecutor};
use everruns_host::{InMemorySessionFileStore, InMemorySessionStorageStore};
use everruns_provider::typed_id::SessionId;
use futures::StreamExt;
use std::collections::VecDeque;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

/// What the mock agent answers one request with.
enum Reply {
    /// An SSE stream of these events, then the body ends.
    Events(Vec<Value>),
    /// These events, then a body that never ends. The flag flips when the
    /// client closes the connection.
    Hang(Vec<Value>, Arc<AtomicBool>),
}

#[derive(Clone, Default)]
struct Mock {
    replies: Arc<Mutex<VecDeque<Reply>>>,
    requests: Arc<Mutex<Vec<(HeaderMap, Value)>>>,
}

impl Mock {
    fn requests(&self) -> Vec<(HeaderMap, Value)> {
        self.requests.lock().unwrap().clone()
    }
}

struct SetOnDrop(Arc<AtomicBool>);

impl Drop for SetOnDrop {
    fn drop(&mut self) {
        self.0.store(true, Ordering::SeqCst);
    }
}

fn frames(events: Vec<Value>) -> Vec<Result<String, std::io::Error>> {
    events
        .into_iter()
        .map(|event| Ok(format!("data: {event}\n\n")))
        .collect()
}

async fn handle(State(mock): State<Mock>, headers: HeaderMap, body: String) -> Response {
    mock.requests
        .lock()
        .unwrap()
        .push((headers, serde_json::from_str(&body).unwrap()));
    let reply = mock.replies.lock().unwrap().pop_front();
    let body = match reply {
        None => return Response::builder().status(500).body(Body::empty()).unwrap(),
        Some(Reply::Events(events)) => Body::from_stream(futures::stream::iter(frames(events))),
        Some(Reply::Hang(events, closed)) => {
            let guard = SetOnDrop(closed);
            let tail = futures::stream::pending::<Result<String, std::io::Error>>().map(move |x| {
                let _ = &guard;
                x
            });
            Body::from_stream(futures::stream::iter(frames(events)).chain(tail))
        }
    };
    Response::builder()
        .status(StatusCode::OK)
        .header("content-type", "text/event-stream")
        .body(body)
        .unwrap()
}

async fn mock_agent(replies: Vec<Reply>) -> (String, Mock) {
    let mock = Mock {
        replies: Arc::new(Mutex::new(replies.into())),
        ..Mock::default()
    };
    let app = Router::new()
        .route("/agent", post(handle))
        .with_state(mock.clone());
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let url = format!("http://{}/agent", listener.local_addr().unwrap());
    tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
    (url, mock)
}

fn started() -> Value {
    json!({ "type": "RUN_STARTED", "threadId": "t", "runId": "remote-1" })
}

fn text(delta: &str) -> Value {
    json!({ "type": "TEXT_MESSAGE_CHUNK", "messageId": "m1", "delta": delta })
}

fn finished(run_id: &str) -> Value {
    json!({ "type": "RUN_FINISHED", "threadId": "t", "runId": run_id })
}

fn agent_config(url: &str, extra: Value) -> Value {
    let mut agent = json!({
        "id": "partner",
        "name": "Partner Agent",
        "url": url,
        "allow_local_urls": true,
    });
    agent
        .as_object_mut()
        .unwrap()
        .extend(extra.as_object().unwrap().clone());
    json!({ "agents": [agent] })
}

fn spawn_tool(config: &Value) -> SpawnAgUiAgentTool {
    AgUiDelegationCapability.validate_config(config).unwrap();
    SpawnAgUiAgentTool::new(AgUiDelegationConfig::from_value(config).unwrap())
}

struct Harness {
    ctx: ToolContext,
    storage: Arc<InMemorySessionStorageStore>,
    registry: Arc<InMemorySessionTaskRegistry>,
}

fn harness() -> Harness {
    let storage = Arc::new(InMemorySessionStorageStore::new());
    let registry = Arc::new(InMemorySessionTaskRegistry::default());
    let ctx = ToolContext::with_stores(
        SessionId::new(),
        Arc::new(InMemorySessionFileStore::new()),
        storage.clone(),
    )
    .with_session_task_registry(registry.clone());
    Harness {
        ctx,
        storage,
        registry,
    }
}

fn spawn_args(mode: &str) -> Value {
    json!({
        "instructions": "Summarise the report",
        "target": { "type": AG_UI_TARGET_TYPE, "id": "partner" },
        "mode": mode,
        "wait_timeout_secs": 10,
    })
}

fn success(result: ToolExecutionResult) -> Value {
    match result {
        ToolExecutionResult::Success(value) => value,
        other => panic!("expected success, got {other:?}"),
    }
}

async fn wait(ctx: &ToolContext, task_id: &str) -> Value {
    success(
        WaitTaskTool
            .execute_with_context(json!({ "task_id": task_id, "timeout_seconds": 10 }), ctx)
            .await,
    )
}

#[tokio::test]
async fn foreground_spawn_streams_text_back_with_the_bearer_secret() {
    let (url, mock) = mock_agent(vec![Reply::Events(vec![
        started(),
        text("Hello "),
        text("world"),
        json!({ "type": "RUN_FINISHED", "threadId": "t", "runId": "remote-1",
                "result": { "ok": true } }),
    ])])
    .await;
    let h = harness();
    h.storage
        .set_secret(h.ctx.session_id, "PARTNER_TOKEN", "tok-secret-123")
        .await
        .unwrap();
    let config = agent_config(
        &url,
        json!({ "bearer_token_secret": "PARTNER_TOKEN", "headers": { "x-tenant": "acme" } }),
    );

    let value = success(
        spawn_tool(&config)
            .execute_with_context(spawn_args("foreground"), &h.ctx)
            .await,
    );
    assert_eq!(value["status"], "completed", "{value}");
    assert_eq!(value["result"], "Hello world");
    assert_eq!(value["remote_run_id"], "remote-1");

    let requests = mock.requests();
    assert_eq!(requests.len(), 1);
    let (headers, body) = &requests[0];
    assert_eq!(headers["authorization"], "Bearer tok-secret-123");
    assert_eq!(headers["x-tenant"], "acme");
    assert_eq!(body["protocolVersion"], "1.0");
    assert_eq!(body["messages"][0]["role"], "user");
    assert_eq!(body["messages"][0]["content"], "Summarise the report");

    let task_id = value["task_id"].as_str().unwrap();
    let task = h
        .registry
        .get(h.ctx.session_id, task_id)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(task.state, SessionTaskState::Succeeded);
    assert_eq!(task.kind, TASK_KIND_EXTERNAL_AG_UI);
    assert_eq!(task.summary.as_deref(), Some("Hello world"));

    // The token is never persisted with the run.
    let run_id = value["agent_run_id"].as_str().unwrap();
    let stored = h
        .storage
        .get_value(h.ctx.session_id, &format!("agent_run:{run_id}"))
        .await
        .unwrap()
        .unwrap();
    assert!(stored.contains("PARTNER_TOKEN"));
    assert!(!stored.contains("tok-secret-123"));
}

#[tokio::test]
async fn background_spawn_is_waitable_via_generic_wait_task() {
    let (url, _mock) = mock_agent(vec![Reply::Events(vec![
        started(),
        text("done"),
        finished("remote-1"),
    ])])
    .await;
    let h = harness();
    let value = success(
        spawn_tool(&agent_config(&url, json!({})))
            .execute_with_context(spawn_args("background"), &h.ctx)
            .await,
    );
    let task_id = value["task_id"].as_str().unwrap();
    // A background run wakes the parent on input requests too.
    let task = h
        .registry
        .get(h.ctx.session_id, task_id)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(task.wake_policy, TaskWakePolicy::OnActivity);

    let waited = wait(&h.ctx, task_id).await;
    assert_eq!(waited["timed_out"], false, "{waited}");
    assert_eq!(waited["task"]["state"], "succeeded");
    assert_eq!(waited["task"]["summary"], "done");
}

#[tokio::test]
async fn interrupt_parks_the_task_and_message_task_resumes_the_thread() {
    let (url, mock) = mock_agent(vec![
        Reply::Events(vec![
            started(),
            text("Need a colour."),
            json!({ "type": "RUN_FINISHED", "threadId": "t", "runId": "remote-1",
                    "outcome": { "type": "interrupt", "interrupts": [
                        { "id": "i1", "reason": "input_required", "message": "Which colour?" }
                    ] } }),
        ]),
        Reply::Events(vec![
            json!({ "type": "RUN_STARTED", "threadId": "t", "runId": "remote-2" }),
            text("Blue it is."),
            finished("remote-2"),
        ]),
    ])
    .await;
    let h = harness();
    let value = success(
        spawn_tool(&agent_config(&url, json!({})))
            .execute_with_context(spawn_args("background"), &h.ctx)
            .await,
    );
    let task_id = value["task_id"].as_str().unwrap().to_string();

    let waited = wait(&h.ctx, &task_id).await;
    assert_eq!(waited["task"]["state"], "awaiting_input", "{waited}");
    let prompt = waited["task"]["input_request"]["prompt"].as_str().unwrap();
    assert!(prompt.contains("[i1] Which colour?"), "{prompt}");

    let delivered = success(
        MessageTaskTool
            .execute_with_context(json!({ "task_id": task_id, "message": "blue" }), &h.ctx)
            .await,
    );
    assert_eq!(delivered["delivery"], "delivered", "{delivered}");

    let waited = wait(&h.ctx, &task_id).await;
    assert_eq!(waited["task"]["state"], "succeeded", "{waited}");
    assert_eq!(waited["task"]["summary"], "Blue it is.");

    let requests = mock.requests();
    assert_eq!(requests.len(), 2);
    let (first, second) = (&requests[0].1, &requests[1].1);
    assert_eq!(second["threadId"], first["threadId"]);
    assert_eq!(second["parentRunId"], "remote-1");
    assert_eq!(
        second["resume"],
        json!([{ "interruptId": "i1", "status": "resolved", "payload": "blue" }])
    );
    // The resumed run carries the conversation so far.
    let roles: Vec<&str> = second["messages"]
        .as_array()
        .unwrap()
        .iter()
        .map(|m| m["role"].as_str().unwrap())
        .collect();
    assert_eq!(roles, ["user", "assistant"]);
}

#[tokio::test]
async fn message_task_is_refused_while_no_input_is_awaited() {
    let (url, _mock) = mock_agent(vec![Reply::Events(vec![
        started(),
        text("ok"),
        finished("remote-1"),
    ])])
    .await;
    let h = harness();
    let value = success(
        spawn_tool(&agent_config(&url, json!({})))
            .execute_with_context(spawn_args("foreground"), &h.ctx)
            .await,
    );
    let delivered = success(
        MessageTaskTool
            .execute_with_context(
                json!({ "task_id": value["task_id"], "message": "more" }),
                &h.ctx,
            )
            .await,
    );
    let delivery = delivered["delivery"].as_str().unwrap();
    assert!(
        delivery.contains("only a run waiting for input"),
        "{delivery}"
    );
}

#[tokio::test]
async fn cancel_task_closes_the_stream() {
    let closed = Arc::new(AtomicBool::new(false));
    let (url, mock) = mock_agent(vec![Reply::Hang(
        vec![started(), text("working...")],
        closed.clone(),
    )])
    .await;
    let h = harness();
    let value = success(
        spawn_tool(&agent_config(&url, json!({})))
            .execute_with_context(spawn_args("background"), &h.ctx)
            .await,
    );
    let task_id = value["task_id"].as_str().unwrap().to_string();
    let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
    while mock.requests().is_empty() {
        assert!(tokio::time::Instant::now() < deadline, "no request arrived");
        tokio::time::sleep(Duration::from_millis(20)).await;
    }

    success(
        CancelTaskTool
            .execute_with_context(json!({ "task_id": task_id }), &h.ctx)
            .await,
    );
    while !closed.load(Ordering::SeqCst) {
        assert!(
            tokio::time::Instant::now() < deadline,
            "the stream was not closed"
        );
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
    let task = h
        .registry
        .get(h.ctx.session_id, &task_id)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(task.state, SessionTaskState::Canceled);
    let run_id = value["agent_run_id"].as_str().unwrap();
    assert_eq!(
        load_run(&h.ctx, run_id).await.unwrap().status,
        AgUiRunStatus::Canceled
    );
}

#[tokio::test]
async fn remote_run_error_fails_the_task() {
    let (url, _mock) = mock_agent(vec![Reply::Events(vec![
        started(),
        json!({ "type": "RUN_ERROR", "message": "quota exceeded", "code": "E_QUOTA" }),
    ])])
    .await;
    let h = harness();
    let value = success(
        spawn_tool(&agent_config(&url, json!({})))
            .execute_with_context(spawn_args("foreground"), &h.ctx)
            .await,
    );
    assert_eq!(value["status"], "failed");
    assert_eq!(value["error"], "quota exceeded (E_QUOTA)");
    let task = h
        .registry
        .get(h.ctx.session_id, value["task_id"].as_str().unwrap())
        .await
        .unwrap()
        .unwrap();
    assert_eq!(task.error.unwrap().kind, "remote_failed");
}

#[tokio::test]
async fn an_http_error_fails_the_run() {
    // No queued reply: the mock answers 500.
    let (url, mock) = mock_agent(vec![]).await;
    let h = harness();
    let value = success(
        spawn_tool(&agent_config(&url, json!({})))
            .execute_with_context(spawn_args("foreground"), &h.ctx)
            .await,
    );
    assert_eq!(value["status"], "failed", "{value}");
    assert!(
        value["error"].as_str().unwrap().contains("HTTP 500"),
        "{value}"
    );
    assert_eq!(mock.requests().len(), 1);
}

#[tokio::test]
async fn a_protocol_violation_fails_the_run() {
    let (url, _mock) = mock_agent(vec![Reply::Events(vec![
        json!({ "type": "TEXT_MESSAGE_START", "messageId": "m1" }),
        started(),
    ])])
    .await;
    let h = harness();
    let value = success(
        spawn_tool(&agent_config(&url, json!({})))
            .execute_with_context(spawn_args("foreground"), &h.ctx)
            .await,
    );
    assert_eq!(value["status"], "failed");
    assert!(
        value["error"]
            .as_str()
            .unwrap()
            .contains("AG-UI protocol violation"),
        "{value}"
    );
}

#[tokio::test]
async fn result_schema_validates_the_run_finished_result() {
    let finished_with = |result: Value| {
        Reply::Events(vec![
            started(),
            json!({ "type": "RUN_FINISHED", "threadId": "t", "runId": "remote-1", "result": result }),
        ])
    };
    let (url, _mock) = mock_agent(vec![
        finished_with(json!({ "answer": "42" })),
        finished_with(json!({ "answer": 42 })),
    ])
    .await;
    let h = harness();
    let tool = spawn_tool(&agent_config(&url, json!({})));
    let mut args = spawn_args("foreground");
    args["result_schema"] = json!({
        "type": "object",
        "properties": { "answer": { "type": "string" } },
        "required": ["answer"],
    });

    let ok = success(tool.execute_with_context(args.clone(), &h.ctx).await);
    assert_eq!(ok["status"], "completed", "{ok}");
    assert!(ok["result_path"].as_str().unwrap().ends_with("result.json"));

    let mismatch = success(tool.execute_with_context(args, &h.ctx).await);
    assert_eq!(mismatch["status"], "failed", "{mismatch}");
    let task = h
        .registry
        .get(h.ctx.session_id, mismatch["task_id"].as_str().unwrap())
        .await
        .unwrap()
        .unwrap();
    assert_eq!(task.error.unwrap().kind, "schema_mismatch");
}

#[tokio::test]
async fn a_missing_secret_fails_before_any_request() {
    let (url, mock) = mock_agent(vec![]).await;
    let h = harness();
    let value = success(
        spawn_tool(&agent_config(
            &url,
            json!({ "bearer_token_secret": "PARTNER_TOKEN" }),
        ))
        .execute_with_context(spawn_args("foreground"), &h.ctx)
        .await,
    );
    assert_eq!(value["status"], "failed");
    assert!(
        value["error"].as_str().unwrap().contains("is not set"),
        "{value}"
    );
    assert!(mock.requests().is_empty());
}

#[tokio::test]
async fn the_network_acl_blocks_before_any_request() {
    let (url, mock) = mock_agent(vec![]).await;
    let h = harness();
    let ctx = h
        .ctx
        .clone()
        .with_network_access(Some(NetworkAccessList::allow_only(["api.example.com"])));
    let value = success(
        spawn_tool(&agent_config(&url, json!({})))
            .execute_with_context(spawn_args("foreground"), &ctx)
            .await,
    );
    assert_eq!(value["status"], "failed");
    assert!(
        value["error"]
            .as_str()
            .unwrap()
            .contains("blocked by network access policy"),
        "{value}"
    );
    assert!(mock.requests().is_empty());
}

#[tokio::test]
async fn spawn_rejects_unknown_agents_bad_modes_and_message_schema() {
    let tool = spawn_tool(&agent_config("http://127.0.0.1:1/agent", json!({})));
    let h = harness();
    let cases = [
        (
            json!({ "instructions": "x", "target": { "type": AG_UI_TARGET_TYPE, "id": "nope" } }),
            "Unknown external AG-UI agent",
        ),
        (
            json!({ "instructions": "x", "target": { "type": AG_UI_TARGET_TYPE } }),
            "target.id",
        ),
        (
            json!({ "instructions": "x", "target": { "type": AG_UI_TARGET_TYPE, "id": "partner" },
                    "mode": "wait" }),
            "Invalid mode",
        ),
        (
            json!({ "instructions": "x", "target": { "type": AG_UI_TARGET_TYPE, "id": "partner" },
                    "message_schema": { "type": "object" } }),
            "message_schema is not supported",
        ),
    ];
    for (args, needle) in cases {
        match tool.execute_with_context(args, &h.ctx).await {
            ToolExecutionResult::ToolError(message) => {
                assert!(message.contains(needle), "{message}")
            }
            other => panic!("expected a tool error containing {needle}, got {other:?}"),
        }
    }
    assert!(
        h.storage
            .list_keys(h.ctx.session_id)
            .await
            .unwrap()
            .is_empty()
    );
}

#[tokio::test]
async fn background_spawn_requires_a_task_registry() {
    let tool = spawn_tool(&agent_config("http://127.0.0.1:1/agent", json!({})));
    let ctx = ToolContext::with_stores(
        SessionId::new(),
        Arc::new(InMemorySessionFileStore::new()),
        Arc::new(InMemorySessionStorageStore::new()),
    );
    match tool
        .execute_with_context(spawn_args("background"), &ctx)
        .await
    {
        ToolExecutionResult::ToolError(message) => {
            assert!(message.contains("requires session_task_registry"))
        }
        other => panic!("expected a tool error, got {other:?}"),
    }
}

#[test]
fn config_validation_refuses_unsafe_urls_credential_headers_and_bad_secret_names() {
    let cap = AgUiDelegationCapability;
    let one = |agent: Value| json!({ "agents": [agent] });
    let base = || json!({ "id": "a", "name": "A", "url": "https://agent.example.com/run" });
    assert!(cap.validate_config(&one(base())).is_ok());
    assert!(cap.validate_config(&Value::Null).is_ok());

    let mut local = base();
    local["url"] = json!("http://127.0.0.1:8080/run");
    assert!(cap.validate_config(&one(local.clone())).is_err());
    local["allow_local_urls"] = json!(true);
    assert!(cap.validate_config(&one(local)).is_ok());

    let mut metadata = base();
    metadata["url"] = json!("http://169.254.169.254/latest");
    assert!(cap.validate_config(&one(metadata)).is_err());

    let mut ftp = base();
    ftp["url"] = json!("ftp://agent.example.com/run");
    ftp["allow_local_urls"] = json!(true);
    assert!(cap.validate_config(&one(ftp)).is_err());

    for header in ["Authorization", "cookie", "Proxy-Authorization"] {
        let mut agent = base();
        agent["headers"] = json!({ header: "x" });
        let err = cap.validate_config(&one(agent)).unwrap_err();
        assert!(err.contains("bearer_token_secret"), "{err}");
    }

    for secret in ["", "has space", "a/b"] {
        let mut agent = base();
        agent["bearer_token_secret"] = json!(secret);
        assert!(cap.validate_config(&one(agent)).is_err(), "{secret:?}");
    }

    let mut extra = base();
    extra["token"] = json!("inline-secret");
    assert!(cap.validate_config(&one(extra)).is_err());

    assert!(
        cap.validate_config(&json!({ "agents": [base(), base()] }))
            .unwrap_err()
            .contains("listed twice")
    );
}

#[test]
fn answers_map_onto_the_open_interrupts() {
    let open = [
        Interrupt::new("i1", "tool_approval"),
        Interrupt::new("i2", "input_required"),
    ];

    let all = resume_from_answer(&open, "yes").unwrap();
    assert_eq!(all.len(), 2);
    assert!(all.iter().all(|e| e.payload == Some(json!("yes"))));

    let keyed = resume_from_answer(&open, r#"{"i2": {"colour": "blue"}}"#).unwrap();
    assert_eq!(keyed[0].interrupt_id, "i1");
    assert_eq!(keyed[0].status, ResumeStatus::Cancelled);
    assert_eq!(keyed[1].status, ResumeStatus::Resolved);
    assert_eq!(keyed[1].payload, Some(json!({ "colour": "blue" })));

    // An object that is not keyed by interrupt ids is one answer for all.
    let object = resume_from_answer(&open, r#"{"approved": true}"#).unwrap();
    assert!(
        object
            .iter()
            .all(|e| e.payload == Some(json!({ "approved": true })))
    );
}

#[tokio::test]
async fn a_lost_stream_is_not_reattached() {
    let h = harness();
    let config = agent_config("http://127.0.0.1:1/agent", json!({}));
    let parsed = AgUiDelegationConfig::from_value(&config).unwrap();
    let mut record = super::run::AgUiRunRecord::new(
        &parsed.agents[0],
        "x".into(),
        SpawnMode::Background,
        true,
        None,
    );
    let task = h
        .registry
        .create(CreateSessionTask {
            session_id: h.ctx.session_id,
            id: None,
            kind: TASK_KIND_EXTERNAL_AG_UI.to_string(),
            display_name: "Partner".into(),
            spec: json!({ "run_id": record.run_id }),
            state: SessionTaskState::Running,
            links: TaskLinks::default(),
            wake_policy: TaskWakePolicy::Silent,
        })
        .await
        .unwrap();
    record.task_id = Some(task.id.clone());
    record.status = AgUiRunStatus::Working;
    save_run(&h.ctx, &record).await.unwrap();

    let executor = AgUiAgentTaskExecutor;
    assert!(!executor.can_reattach());
    let err = executor.start(&task, &h.ctx).await.unwrap_err();
    assert!(err.to_string().contains("cannot be re-attached"), "{err}");
}

#[tokio::test]
async fn capability_contract() {
    let cap = AgUiDelegationCapability;
    assert_eq!(cap.id(), "ag_ui_delegation");
    assert_eq!(cap.dependencies(), vec![SESSION_TASKS_CAPABILITY_ID]);
    assert_eq!(cap.risk_level(), RiskLevel::High);
    assert!(
        everruns_core::session_task::find_task_executor(TASK_KIND_EXTERNAL_AG_UI).is_some(),
        "the executor is registered for its own task kind"
    );
    let config = agent_config(
        "http://127.0.0.1:1/agent",
        json!({ "description": "Reports" }),
    );
    let provider = cap.delegation_target_with_config(&config).unwrap();
    assert_eq!(provider.target_type, AG_UI_TARGET_TYPE);
    let prompt = cap
        .system_prompt_contribution_with_config(
            &SystemPromptContext::without_file_store(SessionId::new()),
            &config,
        )
        .await
        .unwrap();
    assert!(
        prompt.contains("Partner Agent (partner) — Reports"),
        "{prompt}"
    );
    assert!(prompt.contains("message_task"));
}