agy-bridge 0.8.1

Rust bridge for the Google Antigravity SDK (Python) via PyO3
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
/// Agent lifecycle handlers: creation and shutdown.
use std::ffi::CString;

use pyo3::prelude::*;
use tokio::sync::oneshot;

use super::super::{
    AgentId,
    command_loop::{AGY_BRIDGE_GLOBALS_MODULE, AgentRegistry},
    dispatch_rust_policy_confirm, dispatch_rust_tool,
    py_scripts::PYTHON_AGENT_INIT_SCRIPT,
};
use crate::error::Error;

const DISPATCH_RUST_TOOL_ATTR: &str = "dispatch_rust_tool";
const DISPATCH_RUST_HOOK_ATTR: &str = "dispatch_rust_hook";
const DISPATCH_RUST_POLICY_CONFIRM_ATTR: &str = "dispatch_rust_policy_confirm";

/// Prepares the `_agy_bridge_globals` module and registers the Rust tool registry.
fn prepare_agent_globals(py: Python<'_>) -> PyResult<()> {
    let sys = py.import("sys")?;
    let sys_modules = sys.getattr("modules")?;

    let agy_bridge_globals = if sys_modules.contains(AGY_BRIDGE_GLOBALS_MODULE)? {
        sys_modules.get_item(AGY_BRIDGE_GLOBALS_MODULE)?
    } else {
        let types = py.import("types")?;
        let module = types
            .getattr("ModuleType")?
            .call1((AGY_BRIDGE_GLOBALS_MODULE,))?;
        sys_modules.set_item(AGY_BRIDGE_GLOBALS_MODULE, &module)?;
        module
    };

    let globals_module = agy_bridge_globals.cast::<pyo3::types::PyModule>()?;
    let func = pyo3::wrap_pyfunction!(dispatch_rust_tool, globals_module)?;
    agy_bridge_globals.setattr(DISPATCH_RUST_TOOL_ATTR, func)?;

    let hook_func = pyo3::wrap_pyfunction!(crate::runtime::dispatch_rust_hook, globals_module)?;
    agy_bridge_globals.setattr(DISPATCH_RUST_HOOK_ATTR, hook_func)?;

    let confirm_func = pyo3::wrap_pyfunction!(dispatch_rust_policy_confirm, globals_module)?;
    agy_bridge_globals.setattr(DISPATCH_RUST_POLICY_CONFIRM_ATTR, confirm_func)?;

    globals_module.add_class::<crate::policies::PreToolCallDecideHook>()?;
    Ok(())
}

/// Executes the Python initialization script and creates the context and agent coroutine.
fn init_agent_instance(
    py: Python<'_>,
    config_json: &str,
    next_id: u64,
    event_loop: &Py<PyAny>,
) -> PyResult<(Py<PyAny>, Py<PyAny>)> {
    let globals = pyo3::types::PyDict::new(py);
    let c_script = CString::new(PYTHON_AGENT_INIT_SCRIPT).map_err(|e| {
        pyo3::exceptions::PyValueError::new_err(format!(
            "Python init script contains null byte: {e}"
        ))
    })?;
    py.run(c_script.as_c_str(), Some(&globals), None)?;

    let agent_mod = py.import("google.antigravity.agent")?;
    let agent_cls = agent_mod.getattr("Agent")?;
    let init_agent_fn = globals.get_item("init_agent")?.ok_or_else(|| {
        pyo3::exceptions::PyRuntimeError::new_err(
            "init_agent function not found in globals after running PYTHON_AGENT_INIT_SCRIPT",
        )
    })?;

    let val = init_agent_fn.call1((config_json, next_id, agent_cls, event_loop.bind(py)))?;
    let agent_ctx = val.get_item(0)?;
    let aenter_coro = val.get_item(1)?;
    let ctx_py = agent_ctx.clone().unbind();
    let aenter_coro_py = aenter_coro.clone().unbind();
    Ok((ctx_py, aenter_coro_py))
}

/// Initialize a Python agent via the SDK, run `__aenter__`, and register it.
pub(in crate::runtime) async fn handle_create_agent(
    registry: AgentRegistry,
    event_loop: Py<PyAny>,
    next_id: u64,
    config_json: String,
    reply: oneshot::Sender<Result<(AgentId, Vec<RawToolInfo>), Error>>,
) {
    tracing::info!(agent_id = next_id, "Live-SDK: CreateAgent command received");

    let init_result = Python::attach(|py| {
        prepare_agent_globals(py)?;
        init_agent_instance(py, &config_json, next_id, &event_loop)
    });

    let (ctx_py, aenter_coro_py) = match init_result {
        Ok(pair) => pair,
        Err(e) => {
            let err: Error = e.into();
            if let Err(e) = reply.send(Err(err)) {
                tracing::warn!(error = ?e, "CreateAgent reply receiver dropped (config error)");
            }
            return;
        }
    };

    let aenter_fut = match Python::attach(|py| {
        let coro = aenter_coro_py.into_bound(py);
        pyo3_async_runtimes::tokio::into_future(coro)
    }) {
        Ok(fut) => fut,
        Err(e) => {
            let err: Error = e.into();
            if let Err(e) = reply.send(Err(err)) {
                tracing::warn!(error = ?e, "CreateAgent reply receiver dropped (aenter conversion error)");
            }
            return;
        }
    };

    tracing::info!("Live-SDK: awaiting __aenter__");
    let enter_result = aenter_fut.await;

    tracing::info!("Live-SDK: __aenter__ completed");
    match enter_result {
        Ok(agent_instance_py) => {
            let aid = AgentId(next_id);
            let tool_defs = extract_tool_definitions(&agent_instance_py);
            match registry.lock() {
                Ok(mut guard) => {
                    guard.insert(aid, (ctx_py, agent_instance_py));
                    if let Err(e) = reply.send(Ok((aid, tool_defs))) {
                        tracing::warn!(error = ?e, "CreateAgent reply receiver dropped");
                    }
                }
                Err(e) => {
                    tracing::error!(error = %e, "Agent registry mutex poisoned during insert");
                    if let Err(send_err) = reply.send(Err(Error::BackendError {
                        message: "Agent registry mutex poisoned".to_owned(),
                    })) {
                        tracing::warn!(error = ?send_err, "CreateAgent reply receiver dropped (registry poisoned)");
                    }
                }
            }
        }
        Err(e) => {
            let err: Error = e.into();
            if let Err(e) = reply.send(Err(err)) {
                tracing::warn!(error = ?e, "CreateAgent reply receiver dropped (aenter error)");
            }
        }
    }
}

/// Raw tool info extracted from the Python `ToolRunner`.
///
/// This is an intermediate representation — the caller tags each tool with
/// the appropriate [`ToolSource`] and assembles the final [`AvailableTool`] list.
#[derive(Debug, serde::Deserialize)]
pub(crate) struct RawToolInfo {
    pub name: String,
    #[serde(default)]
    pub description: String,
    #[serde(default = "serde_json::Value::default")]
    pub parameter_schema: serde_json::Value,
}

/// Extract tool definitions from the Python agent's `_tool_runner.tools`.
///
/// For each tool callable, extracts:
/// - `__name__` → name
/// - `__doc__` → description (may be empty)
/// - `input_schema` → JSON schema (for `ToolWithSchema` subclasses; missing otherwise)
///
/// Falls back to an empty list if extraction fails (non-fatal).
/// Logs warnings for tools that are missing descriptions or schemas.
fn extract_tool_definitions(agent_py: &Py<PyAny>) -> Vec<RawToolInfo> {
    Python::attach(|py| {
        let agent = agent_py.bind(py);
        let tool_runner = match agent.getattr("_tool_runner") {
            Ok(runner) => runner,
            Err(e) => {
                tracing::warn!(
                    error = %e,
                    "Could not access agent._tool_runner — available_tools will be empty"
                );
                return Vec::new();
            }
        };
        let tools_dict = match tool_runner.getattr("tools") {
            Ok(t) => t,
            Err(e) => {
                tracing::warn!(
                    error = %e,
                    "Could not access _tool_runner.tools — available_tools will be empty"
                );
                return Vec::new();
            }
        };

        // Extract tool info from each callable in the dict by running
        // a small Python helper that reads __name__, __doc__, input_schema.
        let ns = pyo3::types::PyDict::new(py);
        let extract_fn = match py
            .run(
                pyo3::ffi::c_str!(
                    r#"
def _extract(tools_dict):
    import json
    result = []
    for name, fn in tools_dict.items():
        desc = getattr(fn, '__doc__', None) or ''
        schema = getattr(fn, 'input_schema', None) or {}
        result.append({'name': name, 'description': desc, 'parameter_schema': schema})
    return json.dumps(result)
"#
                ),
                None,
                Some(&ns),
            )
            .and_then(|()| {
                ns.get_item("_extract")?.ok_or_else(|| {
                    pyo3::exceptions::PyRuntimeError::new_err(
                        "_extract function not found after running helper script",
                    )
                })
            }) {
            Ok(f) => f,
            Err(e) => {
                tracing::warn!(
                    error = %e,
                    "Failed to define _extract helper — falling back to names only"
                );
                return extract_tool_names_fallback(agent_py);
            }
        };

        let json_str = match extract_fn.call1((tools_dict,)) {
            Ok(result) => match result.extract::<String>() {
                Ok(s) => s,
                Err(e) => {
                    tracing::warn!(
                        error = %e,
                        "Failed to extract JSON string from _extract — falling back to names only"
                    );
                    return extract_tool_names_fallback(agent_py);
                }
            },
            Err(e) => {
                tracing::warn!(
                    error = %e,
                    "Failed to call _extract — falling back to names only"
                );
                return extract_tool_names_fallback(agent_py);
            }
        };

        match serde_json::from_str::<Vec<RawToolInfo>>(&json_str) {
            Ok(infos) => {
                warn_missing_tool_metadata(&infos);
                infos
            }
            Err(e) => {
                tracing::warn!(
                    error = %e,
                    "Failed to deserialize tool definitions JSON — falling back to names only"
                );
                extract_tool_names_fallback(agent_py)
            }
        }
    })
}

/// Log warnings for tools missing a description or parameter schema.
fn warn_missing_tool_metadata(infos: &[RawToolInfo]) {
    for info in infos {
        if info.description.is_empty() {
            tracing::warn!(
                tool = %info.name,
                "Tool has no description — consider adding a docstring"
            );
        }
        if info.parameter_schema.is_null() || info.parameter_schema == serde_json::json!({}) {
            tracing::warn!(
                tool = %info.name,
                "Tool has no parameter schema"
            );
        }
    }
}

/// Fallback: extract just the tool names (no descriptions/schemas) when
/// the full extraction fails.
fn extract_tool_names_fallback(agent_py: &Py<PyAny>) -> Vec<RawToolInfo> {
    Python::attach(|py| {
        let agent = agent_py.bind(py);
        let names: Vec<String> = match agent
            .getattr("_tool_runner")
            .and_then(|r| r.getattr("tool_names"))
            .and_then(|n| n.extract())
        {
            Ok(names) => names,
            Err(e) => {
                tracing::warn!(
                    error = %e,
                    "Failed to extract tool_names in fallback — returning empty tool list"
                );
                Vec::new()
            }
        };
        names
            .into_iter()
            .map(|name| {
                tracing::warn!(
                    tool = %name,
                    "Falling back to name-only tool info (no description or schema)"
                );
                RawToolInfo {
                    name,
                    description: String::new(),
                    parameter_schema: serde_json::Value::Null,
                }
            })
            .collect()
    })
}

/// Remove the agent from the registry and run its `__aexit__` cleanup.
pub(in crate::runtime) async fn handle_shutdown_agent(
    registry: AgentRegistry,
    agent_id: AgentId,
    reply: oneshot::Sender<Result<(), Error>>,
) {
    let Some((ctx_py, _instance)) = (match registry.lock() {
        Ok(mut guard) => guard.remove(&agent_id),
        Err(e) => {
            tracing::error!(error = %e, "Agent registry mutex poisoned during shutdown");
            if let Err(send_err) = reply.send(Err(Error::BackendError {
                message: "Agent registry mutex poisoned".to_owned(),
            })) {
                tracing::warn!(error = ?send_err, "ShutdownAgent reply receiver dropped (registry poisoned)");
            }
            return;
        }
    }) else {
        if let Err(e) = reply.send(Err(Error::BackendError {
            message: format!("Agent ID {agent_id} not found in registry for shutdown"),
        })) {
            tracing::warn!(error = ?e, "ShutdownAgent reply receiver dropped (not found)");
        }
        return;
    };

    // Bridge state cleanup is handled by AgentHandle::shutdown() after this
    // handler returns, ensuring hooks dispatched during __aexit__ (e.g.
    // on_session_end) can still find the hook runner.

    let aexit_coro_res = Python::attach(|py| {
        let ctx_bound = ctx_py.bind(py);
        let none = py.None();
        let coro = ctx_bound.call_method1("__aexit__", (&none, &none, &none))?;
        Ok::<_, PyErr>(coro.clone().unbind())
    });

    let aexit_coro_py = match aexit_coro_res {
        Ok(c) => c,
        Err(e) => {
            let err: Error = e.into();
            if let Err(e) = reply.send(Err(err)) {
                tracing::warn!(agent_id = ?agent_id, error = ?e, "ShutdownAgent reply receiver dropped (aexit coro error)");
            }
            return;
        }
    };

    let aexit_fut = match Python::attach(|py| {
        let coro = aexit_coro_py.into_bound(py);
        pyo3_async_runtimes::tokio::into_future(coro)
    }) {
        Ok(fut) => fut,
        Err(e) => {
            let err: Error = e.into();
            if let Err(e) = reply.send(Err(err)) {
                tracing::warn!(agent_id = ?agent_id, error = ?e, "ShutdownAgent reply receiver dropped (aexit conversion error)");
            }
            return;
        }
    };

    let exit_res = aexit_fut.await;

    // Bridge state cleanup handled by AgentHandle::shutdown().

    match exit_res {
        Ok(_) => {
            if let Err(e) = reply.send(Ok(())) {
                tracing::warn!(agent_id = ?agent_id, error = ?e, "ShutdownAgent reply receiver dropped");
            }
        }
        Err(e) => {
            let err: Error = e.into();
            if let Err(e) = reply.send(Err(err)) {
                tracing::warn!(agent_id = ?agent_id, error = ?e, "ShutdownAgent reply receiver dropped (aexit error)");
            }
        }
    }
}