agy-bridge 0.11.0

Async Rust bridge and native runtime for the Google Antigravity SDK
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
//! Integration tests for the Native backend (local harness over stdio + WebSocket + Protobuf).

#![cfg(feature = "native")]

use std::{
    fs::{self, Permissions},
    os::unix::fs::PermissionsExt,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
    time::Duration,
};

use agy_bridge::{
    AgyBridge,
    config::AgentConfig,
    proto,
    runtime::{BackendLogLevel, NativeRuntime, RuntimeConfig},
};
use futures::{SinkExt, StreamExt};
use prost::Message as _;
use tokio::net::TcpListener;
use tokio_tungstenite::{accept_async, tungstenite::Message};

fn create_mock_harness_binary(port: u16, prefix: &str) -> String {
    let output_config = proto::localharness::OutputConfig {
        port: i32::from(port),
        api_key: "mock-api-key".to_string(),
    };
    let mut out_bytes = Vec::new();
    output_config
        .encode(&mut out_bytes)
        .expect("encode output config");

    let mut frame = Vec::new();
    let len_u32 = u32::try_from(out_bytes.len()).expect("len fits u32");
    frame.extend_from_slice(&len_u32.to_le_bytes());
    frame.extend_from_slice(&out_bytes);

    let mock_bin_path = format!("/tmp/mock_localharness_{prefix}_{port}");
    let payload_path = format!("{mock_bin_path}.dat");
    fs::write(&payload_path, &frame).expect("write mock payload");

    let script_content = format!("#!/bin/sh\ncat '{payload_path}'\nexec sleep 30\n");

    fs::write(&mock_bin_path, script_content).expect("write mock binary");
    fs::set_permissions(&mock_bin_path, Permissions::from_mode(0o755)).expect("set executable");
    mock_bin_path
}

fn cleanup_mock_binary(mock_bin_path: &str) {
    // NOLINT: cleanup in test may fail if mock binary was already cleaned up
    let _ = fs::remove_file(mock_bin_path);
    // NOLINT: cleanup in test may fail if mock payload was already cleaned up
    let _ = fs::remove_file(format!("{mock_bin_path}.dat"));
}

struct MockPolicyHandler;

impl agy_bridge::policies::AskUserHandler for MockPolicyHandler {
    fn confirm(&self, tool_name: &str, _args: &serde_json::Value) -> bool {
        tool_name == "deploy_production"
    }
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
struct CalcParams {
    a: i64,
    b: i64,
}

struct CalcTool;

impl agy_bridge::tools::RustTool for CalcTool {
    type Params = CalcParams;
    const NAME: &'static str = "calculate_sum";
    const DESCRIPTION: &'static str = "Sums two numbers";

    async fn call(
        &self,
        params: Self::Params,
        _ctx: &agy_bridge::tools::ToolContext,
    ) -> Result<agy_bridge::tools::ToolOutput, agy_bridge::tools::ToolError> {
        Ok(agy_bridge::tools::ToolOutput::new(
            (params.a + params.b).to_string(),
        ))
    }
}

#[test]
fn test_native_bridge_builder_and_config() {
    let bridge = AgyBridge::native_builder()
        .backend_log_level(BackendLogLevel::Debug)
        .inter_agent_delay(Duration::from_millis(50))
        .harness_path("/custom/path/to/localharness")
        .build_native()
        .expect("build_native");

    let cfg = bridge.runtime().config();
    assert_eq!(cfg.backend_log_level, BackendLogLevel::Debug);
    assert_eq!(cfg.inter_agent_delay, Duration::from_millis(50));
    assert_eq!(
        cfg.harness_binary_path,
        Some(std::path::PathBuf::from("/custom/path/to/localharness"))
    );
}

#[tokio::test]
async fn test_native_runtime_agent_count() {
    let runtime = Arc::new(NativeRuntime::new(RuntimeConfig::default()));
    let bridge = AgyBridge::new(runtime);

    let count = bridge
        .active_agent_count()
        .await
        .expect("active_agent_count");
    assert_eq!(count, 0);
}

async fn run_mock_chat_session(listener: TcpListener) {
    let (stream, _) = listener.accept().await.expect("accept connection");
    let mut ws = accept_async(stream).await.expect("ws handshake");

    let init_msg = ws.next().await.expect("first message").expect("valid msg");
    let init_text = init_msg.to_text().expect("text msg");
    let _init_event: proto::localharness::InitializeConversationEvent =
        serde_json::from_str(init_text).expect("parse init event");

    let init_resp = proto::localharness::OutputEvent {
        event: Some(
            proto::localharness::output_event::Event::InitializeConversationResponse(
                proto::localharness::InitializeConversationResponse {
                    cascade_id: "native-cascade-42".to_string(),
                    ..Default::default()
                },
            ),
        ),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&init_resp).unwrap().into(),
    ))
    .await
    .expect("send init resp");

    let user_msg = ws.next().await.expect("user msg").expect("valid msg");
    let user_text = user_msg.to_text().expect("text msg");
    let _input_event: proto::localharness::InputEvent =
        serde_json::from_str(user_text).expect("parse input event");

    let step_event = proto::localharness::OutputEvent {
        event: Some(proto::localharness::output_event::Event::StepUpdate(
            proto::localharness::StepUpdate {
                text_delta: "Hello from native backend!".to_string(),
                text: "Hello from native backend!".to_string(),
                ..Default::default()
            },
        )),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&step_event).unwrap().into(),
    ))
    .await
    .expect("send step update");

    let state_event = proto::localharness::OutputEvent {
        event: Some(
            proto::localharness::output_event::Event::TrajectoryStateUpdate(
                proto::localharness::TrajectoryStateUpdate {
                    state: 3,
                    ..Default::default()
                },
            ),
        ),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&state_event).unwrap().into(),
    ))
    .await
    .expect("send state update");
}

#[tokio::test]
async fn test_native_backend_mock_harness_e2e() {
    let listener = TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind listener");
    let port = listener.local_addr().expect("local addr").port();
    let server_task = tokio::spawn(run_mock_chat_session(listener));

    let mock_bin_path = create_mock_harness_binary(port, "basic");

    let bridge = AgyBridge::native_builder()
        .harness_path(&mock_bin_path)
        .build_native()
        .expect("build bridge");

    let agent = bridge
        .agent(AgentConfig::default())
        .await
        .expect("create agent");

    assert_eq!(
        agent.conversation_id(),
        Some("native-cascade-42".to_string())
    );

    let reply = agent.chat_text("Hello").await.expect("chat text");
    assert_eq!(reply, "Hello from native backend!");

    assert_eq!(
        agent.conversation_id(),
        Some("native-cascade-42".to_string())
    );

    agent.shutdown().await.expect("shutdown agent");
    server_task.await.expect("server task completed");

    cleanup_mock_binary(&mock_bin_path);
}

async fn run_mock_tool_session(listener: TcpListener) {
    let (stream, _) = listener.accept().await.expect("accept connection");
    let mut ws = accept_async(stream).await.expect("ws handshake");

    let init_msg = ws.next().await.expect("first message").expect("valid msg");
    let _init_event: proto::localharness::InitializeConversationEvent =
        serde_json::from_str(init_msg.to_text().unwrap()).expect("parse init event");

    let init_resp = proto::localharness::OutputEvent {
        event: Some(
            proto::localharness::output_event::Event::InitializeConversationResponse(
                proto::localharness::InitializeConversationResponse {
                    cascade_id: "tool-cascade-1".to_string(),
                    ..Default::default()
                },
            ),
        ),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&init_resp).unwrap().into(),
    ))
    .await
    .expect("send init resp");

    let _user_msg = ws.next().await.expect("user msg").expect("valid msg");

    let tool_call_event = proto::localharness::OutputEvent {
        event: Some(proto::localharness::output_event::Event::ToolCall(
            proto::localharness::ToolCall {
                id: "call-calc-1".to_string(),
                name: "calculate_sum".to_string(),
                arguments_json: r#"{"a": 20, "b": 22}"#.to_string(),
                arguments: None,
            },
        )),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&tool_call_event).unwrap().into(),
    ))
    .await
    .expect("send tool call");

    let resp_msg = ws
        .next()
        .await
        .expect("tool response msg")
        .expect("valid msg");
    let input_resp: proto::localharness::InputEvent =
        serde_json::from_str(resp_msg.to_text().unwrap()).expect("parse input event");
    if let Some(proto::localharness::input_event::Event::ToolResponse(resp)) = input_resp.event {
        assert_eq!(resp.id, "call-calc-1");
        assert!(resp.response_json.contains("42"));
        assert!(resp.error_message.is_empty());
    } else {
        panic!("Expected ToolResponse event, got {input_resp:?}");
    }

    let final_step_event = proto::localharness::OutputEvent {
        event: Some(proto::localharness::output_event::Event::StepUpdate(
            proto::localharness::StepUpdate {
                text_delta: "The sum is 42.".to_string(),
                text: "The sum is 42.".to_string(),
                ..Default::default()
            },
        )),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&final_step_event).unwrap().into(),
    ))
    .await
    .expect("send final step");

    let state_event = proto::localharness::OutputEvent {
        event: Some(
            proto::localharness::output_event::Event::TrajectoryStateUpdate(
                proto::localharness::TrajectoryStateUpdate {
                    state: 3,
                    ..Default::default()
                },
            ),
        ),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&state_event).unwrap().into(),
    ))
    .await
    .expect("send state update");
}

#[tokio::test]
async fn test_native_backend_custom_tool_dispatch_e2e() {
    let listener = TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind listener");
    let port = listener.local_addr().expect("local addr").port();
    let server_task = tokio::spawn(run_mock_tool_session(listener));

    let mock_bin_path = create_mock_harness_binary(port, "tool");

    let mut registry = agy_bridge::tools::ToolRegistry::new();
    registry.register(CalcTool);

    let bridge = AgyBridge::native_builder()
        .harness_path(&mock_bin_path)
        .build_native()
        .expect("build bridge");

    let agent = bridge
        .agent(AgentConfig::default())
        .tools(registry)
        .await
        .expect("create agent");

    let reply = agent.chat_text("Add 20 and 22").await.expect("chat");
    assert_eq!(reply, "The sum is 42.");

    agent.shutdown().await.expect("shutdown");
    server_task.await.expect("server task completed");

    cleanup_mock_binary(&mock_bin_path);
}

async fn handle_mock_hook_exchange(
    ws: &mut tokio_tungstenite::WebSocketStream<tokio::net::TcpStream>,
) {
    let hook_req_event = proto::localharness::OutputEvent {
        event: Some(proto::localharness::output_event::Event::CallHookRequest(
            proto::localharness::CallHookRequest {
                request_id: "hook-req-1".to_string(),
                name: "pre_turn".to_string(),
                r#type: 3,
                args: Some(proto::localharness::call_hook_request::Args::PreTurnArgs(
                    proto::localharness::PreTurnArgs {
                        user_input: Some(proto::localharness::UserInput {
                            parts: vec![proto::localharness::user_input::Part {
                                part: Some(proto::localharness::user_input::part::Part::Text(
                                    "Run sensitive action".to_string(),
                                )),
                            }],
                        }),
                    },
                )),
            },
        )),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&hook_req_event).unwrap().into(),
    ))
    .await
    .expect("send hook req");

    let hook_resp_msg = ws.next().await.expect("hook resp msg").expect("valid msg");
    let hook_resp_event: proto::localharness::InputEvent =
        serde_json::from_str(hook_resp_msg.to_text().unwrap()).expect("parse hook resp");
    if let Some(proto::localharness::input_event::Event::CallHookResponse(resp)) =
        hook_resp_event.event
    {
        assert_eq!(resp.request_id, "hook-req-1");
        assert!(matches!(
            resp.result,
            Some(proto::localharness::call_hook_response::Result::PreTurnResult(_))
        ));
    } else {
        panic!("Expected CallHookResponse, got {hook_resp_event:?}");
    }
}

async fn handle_mock_policy_exchange(
    ws: &mut tokio_tungstenite::WebSocketStream<tokio::net::TcpStream>,
) {
    let policy_req_event = proto::localharness::OutputEvent {
        event: Some(
            proto::localharness::output_event::Event::PolicyDecisionRequest(
                proto::localharness::PolicyDecisionRequest {
                    request_id: "policy-req-1".to_string(),
                    tool_args: Some(proto::localharness::PreToolArgs {
                        tool_name: "deploy_production".to_string(),
                        arguments_json: r#"{"service":"api"}"#.to_string(),
                        ..Default::default()
                    }),
                    rule_id: "rule-1".to_string(),
                },
            ),
        ),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&policy_req_event).unwrap().into(),
    ))
    .await
    .expect("send policy req");

    let policy_resp_msg = ws
        .next()
        .await
        .expect("policy resp msg")
        .expect("valid msg");
    let policy_resp_event: proto::localharness::InputEvent =
        serde_json::from_str(policy_resp_msg.to_text().unwrap()).expect("parse policy resp");
    if let Some(proto::localharness::input_event::Event::PolicyDecisionResponse(resp)) =
        policy_resp_event.event
    {
        assert_eq!(
            resp.outcome,
            proto::localharness::PolicyEvaluationOutcome::Allow as i32
        );
    } else {
        panic!("Expected PolicyDecisionResponse, got {policy_resp_event:?}");
    }
}

async fn run_mock_hooks_and_policy_session(listener: TcpListener) {
    let (stream, _) = listener.accept().await.expect("accept connection");
    let mut ws = accept_async(stream).await.expect("ws handshake");

    let init_msg = ws.next().await.expect("first message").expect("valid msg");
    let _init_event: proto::localharness::InitializeConversationEvent =
        serde_json::from_str(init_msg.to_text().unwrap()).expect("parse init event");

    let init_resp = proto::localharness::OutputEvent {
        event: Some(
            proto::localharness::output_event::Event::InitializeConversationResponse(
                proto::localharness::InitializeConversationResponse {
                    cascade_id: "hook-cascade-1".to_string(),
                    ..Default::default()
                },
            ),
        ),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&init_resp).unwrap().into(),
    ))
    .await
    .expect("send init resp");

    let _user_msg = ws.next().await.expect("user msg").expect("valid msg");

    handle_mock_hook_exchange(&mut ws).await;
    handle_mock_policy_exchange(&mut ws).await;

    let step_event = proto::localharness::OutputEvent {
        event: Some(proto::localharness::output_event::Event::StepUpdate(
            proto::localharness::StepUpdate {
                text_delta: "Action authorized and completed.".to_string(),
                text: "Action authorized and completed.".to_string(),
                ..Default::default()
            },
        )),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&step_event).unwrap().into(),
    ))
    .await
    .expect("send step");

    let state_event = proto::localharness::OutputEvent {
        event: Some(
            proto::localharness::output_event::Event::TrajectoryStateUpdate(
                proto::localharness::TrajectoryStateUpdate {
                    state: 3,
                    ..Default::default()
                },
            ),
        ),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&state_event).unwrap().into(),
    ))
    .await
    .expect("send state update");
}

#[tokio::test]
async fn test_native_backend_hooks_and_policy_e2e() {
    let listener = TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind listener");
    let port = listener.local_addr().expect("local addr").port();
    let server_task = tokio::spawn(run_mock_hooks_and_policy_session(listener));

    let mock_bin_path = create_mock_harness_binary(port, "hp");

    let pre_turn_called = Arc::new(AtomicBool::new(false));
    let pre_turn_flag = pre_turn_called.clone();

    let mut hooks = agy_bridge::hooks::Hooks::new();
    hooks.on_pre_turn(
        "test_pre_turn",
        move |_ctx: &agy_bridge::hooks::PreTurnContext| {
            pre_turn_flag.store(true, Ordering::SeqCst);
            agy_bridge::hooks::HookResult::allow()
        },
    );

    let bridge = AgyBridge::native_builder()
        .harness_path(&mock_bin_path)
        .build_native()
        .expect("build bridge");

    let agent = bridge
        .agent(AgentConfig::default())
        .hooks(hooks)
        .policy_handler(MockPolicyHandler)
        .await
        .expect("create agent");

    let reply = agent.chat_text("Run sensitive action").await.expect("chat");
    assert_eq!(reply, "Action authorized and completed.");
    assert!(pre_turn_called.load(Ordering::SeqCst));

    agent.shutdown().await.expect("shutdown");
    server_task.await.expect("server task completed");

    cleanup_mock_binary(&mock_bin_path);
}

async fn run_mock_budget_and_subagents_session(listener: TcpListener) {
    let (stream, _) = listener.accept().await.expect("accept connection");
    let mut ws = accept_async(stream).await.expect("ws handshake");

    let init_msg = ws.next().await.expect("first message").expect("valid msg");
    let init_text = init_msg.to_text().expect("text msg");
    let init_event: proto::localharness::InitializeConversationEvent =
        serde_json::from_str(init_text).expect("parse init event");

    let config = init_event.config.expect("config present");
    assert!(config.budget_config.is_some(), "expected budget_config");
    let budget = config.budget_config.unwrap();
    assert_eq!(budget.max_model_calls, 10);
    assert_eq!(budget.max_total_tokens, 50_000);
    assert_eq!(config.agent_behavior, 1); // AUTONOMOUS
    assert_eq!(config.custom_subagents.len(), 1);
    assert_eq!(config.custom_subagents[0].name, "researcher");

    let init_resp = proto::localharness::OutputEvent {
        event: Some(
            proto::localharness::output_event::Event::InitializeConversationResponse(
                proto::localharness::InitializeConversationResponse {
                    cascade_id: "native-cascade-budget".to_string(),
                    ..Default::default()
                },
            ),
        ),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&init_resp).unwrap().into(),
    ))
    .await
    .expect("send init resp");

    let user_msg = ws.next().await.expect("user msg").expect("valid msg");
    let user_text = user_msg.to_text().expect("text msg");
    let _input_event: proto::localharness::InputEvent =
        serde_json::from_str(user_text).expect("parse input event");

    let step_event = proto::localharness::OutputEvent {
        event: Some(proto::localharness::output_event::Event::StepUpdate(
            proto::localharness::StepUpdate {
                text_delta: "Budget-constrained answer".to_string(),
                text: "Budget-constrained answer".to_string(),
                parent_trajectory_id: "native-cascade-budget".to_string(),
                ..Default::default()
            },
        )),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&step_event).unwrap().into(),
    ))
    .await
    .expect("send step update");

    let usage_event = proto::localharness::OutputEvent {
        event: Some(proto::localharness::output_event::Event::UsageUpdate(
            proto::localharness::UsageUpdate {
                total: Some(proto::localharness::UsageMetadata {
                    prompt_token_count: 100,
                    candidates_token_count: 50,
                    total_token_count: 150,
                    prompt_tokens_details: vec![proto::localharness::ModalityTokenCount {
                        modality: 1, // TEXT
                        token_count: 100,
                    }],
                    ..Default::default()
                }),
                ..Default::default()
            },
        )),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&usage_event).unwrap().into(),
    ))
    .await
    .expect("send usage update");

    let state_event = proto::localharness::OutputEvent {
        event: Some(
            proto::localharness::output_event::Event::TrajectoryStateUpdate(
                proto::localharness::TrajectoryStateUpdate {
                    state: 3,
                    stop_reason: 1, // MAX_MODEL_CALLS_EXCEEDED
                    ..Default::default()
                },
            ),
        ),
        ..Default::default()
    };
    ws.send(Message::Text(
        serde_json::to_string(&state_event).unwrap().into(),
    ))
    .await
    .expect("send state update");
}

#[tokio::test]
async fn test_native_backend_budget_and_subagents_e2e() {
    let listener = TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind listener");
    let port = listener.local_addr().expect("local addr").port();
    let server_task = tokio::spawn(run_mock_budget_and_subagents_session(listener));

    let mock_bin_path = create_mock_harness_binary(port, "bs");

    let bridge = AgyBridge::native_builder()
        .harness_path(&mock_bin_path)
        .build_native()
        .expect("build bridge");

    let config = AgentConfig::builder()
        .budget_config(
            agy_bridge::config::BudgetConfig::builder()
                .max_model_calls(10)
                .max_total_tokens(50_000)
                .build(),
        )
        .capabilities(
            agy_bridge::config::CapabilitiesConfig::builder()
                .agent_behavior(agy_bridge::config::AgentBehavior::Autonomous)
                .max_subagent_depth(2)
                .allowed_subagents(vec!["researcher".to_string()])
                .build(),
        )
        .subagents(vec![
            agy_bridge::config::SubagentConfig::builder()
                .name("researcher")
                .description("Research subagent")
                .build(),
        ])
        .build();

    let agent = bridge.agent(config).await.expect("create agent");

    let reply = agent.chat_text("Perform budget task").await.expect("chat");
    assert_eq!(reply, "Budget-constrained answer");

    agent.shutdown().await.expect("shutdown");
    server_task.await.expect("server task completed");

    cleanup_mock_binary(&mock_bin_path);
}