Skip to content

Incremental engine for long horizon agents 🌟 Star if you like it!

10.1 KB
Raw Blame
use std::{
    hash::{Hash, Hasher},
    pin::Pin,
    sync::{Arc, OnceLock},
    time::Duration,
};

use crate::prelude::*;

use cocoindex_core::engine::runtime::{
    cancel_all, get_runtime, reset_global_cancellation, shutdown_runtime,
};
use cocoindex_py_utils::{cerror_to_pyerr, from_py_future};
use futures::FutureExt;
use pyo3::{call::PyCallArgs, exceptions::PyException};
use pyo3_async_runtimes::TaskLocals;
use tokio_util::task::AbortOnDropHandle;

pub struct PythonObjects {
    pub serialize_fn: Py<PyAny>,
    /// `ChildSlot(core_slot)`: wraps a `ChildTargetSlot` for a container sink.
    pub child_slot_wrapper_fn: Py<PyAny>,
    /// `_TypedTargetHandlerWrapper(handler)`: what a child handler is held as,
    /// whether a slot or a `child_handler=` factory provided it.
    pub typed_target_handler_wrapper_fn: Py<PyAny>,
    pub non_existence: Py<PyAny>,
    pub target_state_codec: crate::target_state_codec::TargetStateCodec,
    /// `data_dep.validate_stored_data_deps`: validates a stored memo's
    /// data-dependency records with no processor (see
    /// `EngineProfile::validate_data_deps`).
    pub validate_data_deps_fn: PyCallback,
}

impl PythonObjects {
    pub fn serialize<'py>(
        &self,
        py: Python<'py>,
        value: &Bound<'py, PyAny>,
    ) -> Result<bytes::Bytes> {
        (|| -> PyResult<bytes::Bytes> {
            Ok(self
                .serialize_fn
                .call(py, (value,), None)?
                .extract::<bytes::Bytes>(py)?)
        })()
        .from_py_result()
    }
}

static PY_OBJECTS: OnceLock<std::mem::ManuallyDrop<PythonObjects>> = OnceLock::new();

// One call at import, with keyword arguments from Python; a struct for them
// would be a pyclass with a single use.
#[allow(clippy::too_many_arguments)]
#[pyfunction]
pub fn init_runtime(
    package_id: String,
    lang: String,
    serialize_fn: Py<PyAny>,
    child_slot_wrapper_fn: Py<PyAny>,
    typed_target_handler_wrapper_fn: Py<PyAny>,
    non_existence: Py<PyAny>,
    target_state_codec: Bound<'_, PyAny>,
    validate_data_deps_fn: Py<PyAny>,
) -> PyResult<()> {
    let target_state_codec = crate::target_state_codec::TargetStateCodec::new(&target_state_codec)?;
    if pyo3_async_runtimes::tokio::init_with_runtime(get_runtime()).is_err() {
        return Err(PyException::new_err(
            "Failed to initialize Tokio runtime: already initialized",
        ));
    }
    cocoindex_core::telemetry::init(package_id, lang);
    PY_OBJECTS
        .set(std::mem::ManuallyDrop::new(PythonObjects {
            serialize_fn,
            child_slot_wrapper_fn,
            typed_target_handler_wrapper_fn,
            non_existence,
            target_state_codec,
            validate_data_deps_fn: PyCallback::Async(Arc::new(validate_data_deps_fn)),
        }))
        .map_err(|_| PyException::new_err("Failed to set Python objects: already initialized"))?;
    Ok(())
}

/// Shut the Tokio runtime down with the GIL released. The tasks it drops can
/// hold a Python awaitable (a `process_live` still running, for one), and
/// dropping one takes the GIL to cancel its Python task: with the GIL held
/// here, those drops wait out the shutdown timeout and keep their worker
/// threads running past it.
#[pyfunction]
pub fn shutdown_tokio_runtime(py: Python<'_>) {
    py.detach(shutdown_runtime);
}

/// How often [`block_on_interruptibly`] wakes to run Python's signal handlers.
const SIGNAL_CHECK_INTERVAL: Duration = Duration::from_millis(100);

/// Block on `work` on the engine runtime with the GIL released, as a blocking
/// entry point (`App.update_blocking`, `App.drop_blocking`) does, and stay
/// responsive to Ctrl+C while waiting.
///
/// CPython only records a signal when it arrives; its Python handler (the one
/// raising `KeyboardInterrupt` for SIGINT) runs once the main thread runs
/// Python code again, which a wait on work that never ends (a live update)
/// never does. So the wait wakes every [`SIGNAL_CHECK_INTERVAL`] to run the
/// pending handlers (`check_signals`, which does nothing off the main thread).
/// When a handler raises, `stop` is called to cancel the work, and the wait
/// goes on while the work winds down, so a progress display ends with its
/// final line; then the handler's exception is raised in place of the work's
/// result. A handler raising again before that (a second Ctrl+C) ends the wait
/// at once with its exception, leaving the work to finish on the runtime.
pub fn block_on_interruptibly<T: Send>(
    py: Python<'_>,
    work: impl Future<Output = PyResult<T>> + Send,
    stop: impl FnOnce(),
) -> PyResult<T> {
    let mut work = std::pin::pin!(work);
    let interrupt = loop {
        if let Some(result) = wait_a_while(py, work.as_mut()) {
            return result;
        }
        if let Err(err) = py.check_signals() {
            break err;
        }
    };
    stop();
    loop {
        if wait_a_while(py, work.as_mut()).is_some() {
            return Err(interrupt);
        }
        py.check_signals()?;
    }
}

/// Poll `work` with the GIL released for up to [`SIGNAL_CHECK_INTERVAL`];
/// `None` if it has not finished by then.
fn wait_a_while<F>(py: Python<'_>, work: Pin<&mut F>) -> Option<F::Output>
where
    F: Future + Send,
    F::Output: Send,
{
    py.detach(|| {
        get_runtime()
            .block_on(async { tokio::time::timeout(SIGNAL_CHECK_INTERVAL, work).await.ok() })
    })
}

/// Cancel the global cancellation token, causing all in-flight operations to
/// exit promptly.  Safe to call from signal handlers.
#[pyfunction]
#[pyo3(name = "cancel_all")]
pub fn py_cancel_all() {
    cancel_all();
}

/// Replace the cancelled global token with a fresh one so new operations can
/// proceed.  Called automatically at the start of each CLI command.
#[pyfunction]
#[pyo3(name = "reset_global_cancellation")]
pub fn py_reset_global_cancellation() {
    reset_global_cancellation();
}

pub fn python_objects() -> &'static PythonObjects {
    // ManuallyDrop<T> implements Deref<Target = T>, so &ManuallyDrop<T> coerces to &T.
    PY_OBJECTS.get().expect("Python objects not initialized")
}

#[pyclass(name = "AsyncContext", from_py_object)]
#[derive(Clone)]
pub struct PyAsyncContext(pub Arc<TaskLocals>);

impl PartialEq for PyAsyncContext {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

impl Eq for PyAsyncContext {}

impl Hash for PyAsyncContext {
    fn hash<H: Hasher>(&self, state: &mut H) {
        Arc::as_ptr(&self.0).hash(state);
    }
}

#[pymethods]
impl PyAsyncContext {
    #[new]
    pub fn new(event_loop: Bound<PyAny>) -> Self {
        Self(Arc::new(pyo3_async_runtimes::TaskLocals::new(event_loop)))
    }
}

#[derive(Clone)]
pub enum PyCallback {
    Sync(Arc<Py<PyAny>>),
    Async(Arc<Py<PyAny>>),
}

impl PyCallback {
    /// The Python callable, however it is invoked.
    pub fn object(&self) -> &Py<PyAny> {
        match self {
            PyCallback::Sync(callback) | PyCallback::Async(callback) => callback,
        }
    }

    pub fn call<A>(
        &self,
        host_runtime_ctx: &PyAsyncContext,
        args: A,
    ) -> Result<impl Future<Output = Result<Py<PyAny>>> + Send + 'static>
    where
        A: for<'py> PyCallArgs<'py> + Send + 'static,
    {
        let boxed_fut = match self {
            PyCallback::Sync(sync_fn) => {
                let sync_fn = sync_fn.clone();
                let result_fut = AbortOnDropHandle::new(
                    get_runtime()
                        .spawn_blocking(move || Python::attach(|py| sync_fn.call(py, args, None))),
                );
                async move {
                    result_fut.await.map_err(|err| {
                        PyException::new_err(format!("Failed to call Python function: {err:?}"))
                    })?
                }
                .boxed()
            }
            PyCallback::Async(async_fn) => Python::attach(|py| {
                let result_coro = async_fn.call(py, args, None)?;
                from_py_future(py, &host_runtime_ctx.0, result_coro.into_bound(py))
            })?
            .boxed(),
        };
        Ok(boxed_fut.map(|r| r.from_py_result()))
    }
}

/// Wrap an optional Python async callback `(exc) -> Awaitable[None]`
/// as the Rust `OnError` closure expected by `Component::run_in_background`,
/// `Component::delete`, and the live-component controller.
///
/// Shared by `mount_async` (single-shot background mount), `update_full_async`,
/// `update_async`, and `delete_async` (live-component ops). The propagation
/// semantics are uniform across all of them:
///
/// - The engine error is converted with [`cerror_to_pyerr`], the same mapping
///   foreground paths (`use_mount`, `handle.ready()`) use: a Python-originated
///   failure reaches the callback as its original exception object (type +
///   traceback intact); engine-native failures map to the usual Python types.
/// - Coroutine returns normally (handler chain swallows) β†’ `Ok(())` β†’
///   spawned task swallows.
/// - Coroutine raises (chain exhausted via raises) β†’ `Err(...)` β†’ spawned
///   task propagates via `handle.ready()`. The error is the `HostedPyErr`
///   produced by `PyCallback::call`, so the exception the handler raised
///   re-surfaces in Python with its type intact.
/// - Dispatch-level failures (couldn't schedule the coroutine) are logged
///   and converted to `Err` so they surface rather than disappearing.
pub fn build_on_error(
    host_runtime_ctx: PyAsyncContext,
    handler_callback: Option<Py<PyAny>>,
) -> Option<cocoindex_core::engine::component::OnError> {
    let handler_callback = handler_callback?;
    let cb = PyCallback::Async(Arc::new(handler_callback));
    Some(Arc::new(move |err: cocoindex_utils::prelude::Error| {
        let cb = cb.clone();
        let host_runtime_ctx = host_runtime_ctx.clone();
        Box::pin(async move {
            let exc = Python::attach(|py| cerror_to_pyerr(err).into_value(py));
            let fut = match cb.call(&host_runtime_ctx, (exc,)) {
                Ok(fut) => fut,
                Err(e) => {
                    error!("exception handler dispatch failed:\n{e:?}");
                    return Err(e.context("exception handler dispatch failed"));
                }
            };
            fut.await.map(|_| ())
        })
    }))
}