Skip to main content

agent_block_core/bridge/task/
mod.rs

1//! `std.task` thin adapter.
2//!
3//! Bridge implementation moved to the `mlua-batteries` crate
4//! (`mlua_batteries::task`).  This module only resolves the host's
5//! environment variables into a [`mlua_batteries::task::TaskConfig`]
6//! before delegating to [`mlua_batteries::task::register_with`].
7//!
8//! # Environment variables
9//!
10//! - `AGENT_BLOCK_TASK_DRIVER` — `async_fn` (default), `async`, or
11//!   `coroutine`.  Selects the default driver used by `std.task.spawn`
12//!   when the caller does not pass `opts.driver`.  Unparseable values
13//!   silently fall back to `async_fn` (mirrors `AGENT_BLOCK_TASK_GRACE_MS`).
14//! - `AGENT_BLOCK_TASK_GRACE_MS` — default grace window (cooperative
15//!   cancel → hard abort) used by `std.task.with_timeout` when the caller
16//!   does not pass `opts.grace_ms`.  Default: 1000 ms.  Set to 0 for
17//!   strict / immediate-abort semantics.  Parsing is delegated to
18//!   [`crate::bridge::config::task_grace_ms`], which `tracing::warn!`s on
19//!   unparseable values and falls back to the default.
20
21use mlua::prelude::*;
22use mlua_batteries::task::{Driver, TaskConfig};
23
24use crate::bridge::config;
25
26fn parse_driver_env() -> Driver {
27    match std::env::var("AGENT_BLOCK_TASK_DRIVER").ok().as_deref() {
28        Some("coroutine") => Driver::Coroutine,
29        _ => Driver::AsyncFn,
30    }
31}
32
33pub fn register(lua: &Lua) -> LuaResult<()> {
34    let cfg = TaskConfig {
35        default_driver: parse_driver_env(),
36        grace_ms: config::task_grace_ms(),
37    };
38    mlua_batteries::task::register_with(lua, cfg)
39}