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
//! Agent lifecycle, initializing-hook cleanup, `chat_text()`, and subagent
//! lifecycle tests for the mock runtime.

use super::*;

// ── Agent lifecycle tests ────────────────────────────────────────

#[tokio::test]
async fn create_chat_shutdown_lifecycle() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let agent = AgentHandle::new(rt, test_config(), None, None, None)
        .await
        .expect("create should succeed");

    assert!(agent.is_started());
    assert!(agent.id() > 0);

    {
        let mut response = agent.chat("Hello").await.expect("chat should succeed");
        if let Some(mut rx) = response.take_tool_call_stream() {
            let call = rx.recv().await.expect("should get tool call");
            assert_eq!(call.name, "add_numbers");
        }
    }

    agent.shutdown().await.expect("shutdown should succeed");
    assert!(!agent.is_started());
}

#[tokio::test]
async fn create_with_invalid_config_returns_error() {
    let rt = Arc::new(ToolAwareMockRuntime::with_create_failure());
    let result = AgentHandle::new(rt, test_config(), None, None, None).await;

    match result {
        Err(Error::BackendError { message }) => {
            assert!(message.contains("invalid config"));
        }
        Err(other) => panic!("Expected BackendError, got: {other:?}"),
        Ok(_) => panic!("Expected error, got Ok"),
    }
}

#[tokio::test]
async fn chat_quota_error_is_single_shot() {
    // agy-bridge is single-shot by design: a quota error is surfaced to the
    // caller immediately, with no internal retry. The caller owns retrying
    // (e.g. via `agent_resilience::backoff` in the consumer).
    let rt = Arc::new(ToolAwareMockRuntime::new());
    rt.fail_quota.store(true, Ordering::SeqCst);

    let agent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
        .await
        .expect("create should succeed");

    let result = agent.chat("Hello").await;
    assert!(
        matches!(result, Err(Error::QuotaExceeded { .. })),
        "expected immediate QuotaExceeded (single-shot), got {result:?}"
    );

    agent.shutdown().await.expect("shutdown should succeed");
}

#[tokio::test]
async fn conversation_id_tracking() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let agent = AgentHandle::new(rt, test_config(), None, None, None)
        .await
        .expect("create should succeed");

    assert!(agent.conversation_id().is_none());

    // Simulate the local harness assigning a conversation id: the Python bridge
    // syncs it into the shared bridge-state Arc via `set_agent_conversation_id`,
    // which the handle observes through the same Arc.
    crate::runtime::set_agent_conversation_id(agent.id(), "conv_abc123".to_owned())
        .expect("sync conversation id");
    assert_eq!(agent.conversation_id().as_deref(), Some("conv_abc123"));

    agent.shutdown().await.expect("shutdown should succeed");
}

#[tokio::test]
#[cfg(feature = "python")]
async fn ffi_session_start_does_not_inject_session_id_as_conversation_id() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let agent = AgentHandle::new(rt, test_config(), None, None, None)
        .await
        .expect("create should succeed");

    assert!(agent.conversation_id().is_none());

    // Simulate the dispatch_rust_hook callback for on_session_start.
    // on_session_start is observe-only: its `session_id` may be a fabricated
    // fallback (workspace basename or "default_session"), NOT the real SDK
    // conversation id, so it must not be injected. The authentic id arrives
    // separately via `set_agent_conversation_id`; conversation_id stays None
    // until then.
    let ctx = crate::hooks::OnSessionStartContext {
        session: crate::hooks::SessionContext {
            session_id: "dynamically-generated-session-123".to_owned(),
            agent_id: agent.id(),
            started_at: std::time::SystemTime::now(),
        },
    };
    let ctx_json = serde_json::to_string(&ctx).unwrap();

    // Simulate hook callback execution using the internal dispatch function
    let hook_runner = {
        let map = crate::runtime::bridge_state().read().unwrap();
        let entry = map.get(&agent.id()).unwrap();
        Arc::clone(entry.hook_runner.as_ref().unwrap())
    };

    crate::runtime::ffi_dispatch::dispatch_hook_by_name(
        agent.id(),
        &hook_runner,
        "on_session_start",
        &ctx_json,
    )
    .unwrap();

    // conversation_id must remain None — session_id is NOT a conversation handle
    assert!(
        agent.conversation_id().is_none(),
        "on_session_start must NOT inject session_id as conversation_id"
    );

    agent.shutdown().await.expect("shutdown should succeed");
}

#[tokio::test]
async fn double_shutdown_is_idempotent() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let agent = AgentHandle::new(rt, test_config(), None, None, None)
        .await
        .expect("create should succeed");

    agent
        .shutdown()
        .await
        .expect("first shutdown should succeed");
    assert!(!agent.is_started());

    agent
        .shutdown()
        .await
        .expect("second shutdown should succeed");
}

#[tokio::test]
async fn drop_without_shutdown_calls_try_shutdown() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let agent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
        .await
        .expect("create should succeed");

    assert!(!rt.try_shutdown_called.load(Ordering::SeqCst));
    drop(agent);
    assert!(
        rt.try_shutdown_called.load(Ordering::SeqCst),
        "Drop should call try_shutdown_agent"
    );
}

#[tokio::test]
async fn drop_after_shutdown_does_not_call_try_shutdown() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let agent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
        .await
        .expect("create should succeed");

    agent.shutdown().await.expect("shutdown");
    assert!(!rt.try_shutdown_called.load(Ordering::SeqCst));

    drop(agent);
    assert!(
        !rt.try_shutdown_called.load(Ordering::SeqCst),
        "Drop after explicit shutdown should NOT call try_shutdown_agent"
    );
}

// ── InitializingHookGuard cleanup tests ────────────────────────────
//
// These guard the lock-free, per-agent-keyed creation path: the entry a
// creating agent installs in `initializing_hook_runners()` must never
// outlive `AgentHandle::new`, on ANY exit path. All tests key on a
// globally-unique `next_agent_id()` so they are deterministic even when
// run in parallel with other tests that share the global registry.

/// Dropping the guard removes exactly its own entry and nothing else.
#[tokio::test]
async fn initializing_hook_guard_removes_entry_on_drop() {
    let id = crate::runtime::next_agent_id();
    let other = crate::runtime::next_agent_id();
    let hooks = Arc::new(crate::hooks::Hooks::new());

    {
        let mut map = crate::runtime::initializing_hook_runners()
            .write()
            .expect("registry writable");
        map.insert(id, Arc::clone(&hooks));
        map.insert(other, Arc::clone(&hooks));
    }

    {
        let _guard = super::super::InitializingHookGuard(id);
        assert!(
            crate::runtime::initializing_hook_runners()
                .read()
                .expect("registry readable")
                .contains_key(&id),
            "entry must exist while guard is alive"
        );
    } // guard drops here

    let map = crate::runtime::initializing_hook_runners()
        .read()
        .expect("registry readable");
    assert!(
        !map.contains_key(&id),
        "guard drop must remove its own entry"
    );
    assert!(
        map.contains_key(&other),
        "guard drop must NOT remove unrelated entries"
    );
    drop(map);

    // Cleanup the unrelated entry we inserted.
    crate::runtime::initializing_hook_runners()
        .write()
        .expect("registry writable")
        .remove(&other);
}

/// A successful `AgentHandle::new` must leave no lingering entry for its ID.
#[tokio::test]
async fn successful_create_leaves_no_initializing_entry() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let agent = AgentHandle::new(rt, test_config(), None, None, None)
        .await
        .expect("create should succeed");

    assert!(
        !crate::runtime::initializing_hook_runners()
            .read()
            .expect("registry readable")
            .contains_key(&agent.id()),
        "successful create must not leave an initializing hook entry"
    );

    agent.shutdown().await.expect("shutdown should succeed");
}

/// A FAILED `AgentHandle::new` must still clean up its initializing entry —
/// the RAII-on-error path. The mock records the exact `agent_id` it was
/// handed, so we can assert cleanup for that precise key.
#[tokio::test]
async fn failed_create_cleans_up_initializing_entry() {
    let rt = Arc::new(ToolAwareMockRuntime::with_create_failure());
    let result = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None).await;
    assert!(result.is_err(), "create was configured to fail");

    let failed_id = rt
        .last_create_id
        .lock()
        .expect("mock id mutex")
        .expect("create_agent must have recorded an id before failing");

    assert!(
        !crate::runtime::initializing_hook_runners()
            .read()
            .expect("registry readable")
            .contains_key(&failed_id),
        "failed create must not leak an initializing hook entry for id {failed_id}"
    );
}

// ── chat_text() tests ──────────────────────────────────────────────

#[tokio::test]
async fn chat_text_returns_text() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let agent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
        .await
        .expect("create agent");

    let text = agent.chat_text("Hello").await.expect("chat_text");
    assert!(!text.is_empty());

    agent.shutdown().await.expect("shutdown");
}

#[tokio::test]
async fn cancel_completes_successfully() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let agent = AgentHandle::new(rt, test_config(), None, None, None)
        .await
        .expect("create agent");
    agent.cancel().await.expect("cancel should succeed");
}

#[tokio::test]
async fn wait_for_idle_completes_successfully() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let agent = AgentHandle::new(rt, test_config(), None, None, None)
        .await
        .expect("create agent");
    agent
        .wait_for_idle()
        .await
        .expect("wait_for_idle should succeed");
}

#[tokio::test]
async fn send_completes_successfully() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let agent = AgentHandle::new(rt, test_config(), None, None, None)
        .await
        .expect("create agent");
    agent
        .send("fire-and-forget message")
        .await
        .expect("send should succeed");
}

#[tokio::test]
async fn signal_idle_completes_successfully() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let agent = AgentHandle::new(rt, test_config(), None, None, None)
        .await
        .expect("create agent");
    agent
        .signal_idle()
        .await
        .expect("signal_idle should succeed");
}

#[tokio::test]
async fn wait_for_wakeup_returns_false_on_mock_timeout() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let agent = AgentHandle::new(rt, test_config(), None, None, None)
        .await
        .expect("create agent");
    let woken = agent
        .wait_for_wakeup(Duration::from_secs(1))
        .await
        .expect("wait_for_wakeup should succeed");
    assert!(!woken, "mock should return false (timeout)");
}

// ── Audit 4: Subagent lifecycle tests ──────────────────────────────

#[tokio::test]
async fn spawn_subagent_creates_child() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let parent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
        .await
        .expect("create parent");
    let parent_id = parent.id();

    let child = parent
        .spawn_subagent(test_config(), None)
        .await
        .expect("spawn subagent");
    let child_id = child.id();

    // Child should have a distinct ID.
    assert_ne!(parent_id, child_id);
    assert!(child.is_started());
}

#[tokio::test]
async fn spawn_subagent_with_registry_populates_tools() {
    #[derive(serde::Deserialize, schemars::JsonSchema)]
    struct Params {
        x: i32,
    }
    struct TestTool;
    impl crate::tools::RustTool for TestTool {
        type Params = Params;
        const NAME: &'static str = "test_tool";
        const DESCRIPTION: &'static str = "A test tool";
        async fn call(
            &self,
            params: Params,
            _ctx: &crate::tools::ToolContext,
        ) -> Result<crate::tools::ToolOutput, crate::tools::ToolError> {
            assert!(params.x > 0, "expected positive x, got {}", params.x);
            Ok("ok".into())
        }
    }

    let rt = Arc::new(ToolAwareMockRuntime::new());
    let parent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
        .await
        .expect("create parent");

    let mut registry = crate::tools::ToolRegistry::new();
    registry.register(TestTool);

    let child = parent
        .spawn_subagent(test_config(), registry)
        .await
        .expect("spawn subagent with registry");

    // The child's config should have the tool definition from the registry.
    assert_eq!(child.config().tools.len(), 1);
    assert_eq!(child.config().tools[0].name, "test_tool");
}

#[tokio::test]
async fn subagent_shutdown_lifecycle() {
    let rt = Arc::new(ToolAwareMockRuntime::new());
    let parent = AgentHandle::new(Arc::clone(&rt), test_config(), None, None, None)
        .await
        .expect("create parent");

    let child = parent
        .spawn_subagent(test_config(), None)
        .await
        .expect("spawn subagent");

    // Shut down the child.
    child.shutdown().await.expect("shutdown child");
    assert!(!child.is_started());
}