feat(foreign): structured invoke, presets, probe and config

F0/F1: safe execution core (exec gate, bounded IO, process-group timeout), structured invoke records, run options, 15 language presets, register/unset/probe/config, view stderr/exit/ms, and node/bash gold tests.
This commit is contained in:
cflmy committed 2026-10-18 01:26:53 +08:00
1 parent c21a28c3bc
commit e32ff8f6b3
21 files changed
+2301 -282

No files matched your search

+141 -3
View File
@@ -202,6 +202,11 @@ pub enum HostFn {
MetaGet = 193,
ModuleSource = 194,
ArtifactKind = 195,
ForeignInvoke = 196,
ForeignProbe = 197,
ForeignRegister = 198,
ForeignUnset = 199,
ForeignLoadConfig = 200,
}
@@ -394,6 +399,11 @@ impl HostFn {
193 => Self::MetaGet,
194 => Self::ModuleSource,
195 => Self::ArtifactKind,
196 => Self::ForeignInvoke,
197 => Self::ForeignProbe,
198 => Self::ForeignRegister,
199 => Self::ForeignUnset,
200 => Self::ForeignLoadConfig,
_ => return None,
})
}
@@ -470,6 +480,11 @@ impl HostFn {
"host_foreign_set_cmd" => Self::ForeignSetCmd,
"host_foreign_run" => Self::ForeignRun,
"host_foreign_run_lang" => Self::ForeignRunLang,
"host_foreign_invoke" | "foreign_invoke" => Self::ForeignInvoke,
"host_foreign_probe" | "foreign_probe" => Self::ForeignProbe,
"host_foreign_register" | "foreign_register" => Self::ForeignRegister,
"host_foreign_unset" | "foreign_unset" => Self::ForeignUnset,
"host_foreign_load_config" | "foreign_load_config" => Self::ForeignLoadConfig,
"host_foreign_langs" => Self::ForeignLangs,
"host_dotenv_load" | "load_dotenv" => Self::DotenvLoad,
"host_http_request" | "http_request" => Self::HttpRequest,
@@ -666,6 +681,11 @@ impl HostFn {
Self::ForeignSetCmd => "host_foreign_set_cmd",
Self::ForeignRun => "host_foreign_run",
Self::ForeignRunLang => "host_foreign_run_lang",
Self::ForeignInvoke => "host_foreign_invoke",
Self::ForeignProbe => "host_foreign_probe",
Self::ForeignRegister => "host_foreign_register",
Self::ForeignUnset => "host_foreign_unset",
Self::ForeignLoadConfig => "host_foreign_load_config",
Self::ForeignLangs => "host_foreign_langs",
Self::DotenvLoad => "host_dotenv_load",
Self::HttpRequest => "host_http_request",
@@ -832,6 +852,9 @@ impl HostFn {
Self::ForeignSetCmd => &["lang", "cmd"],
Self::ForeignRun => &["code"],
Self::ForeignRunLang => &["lang", "source"],
Self::ForeignInvoke => &[],
Self::ForeignProbe | Self::ForeignLoadConfig => &[],
Self::ForeignRegister | Self::ForeignUnset => &["lang"],
Self::ForeignLangs => &[],
Self::PluginLoad => &["path"],
Self::PluginUnload | Self::PluginList => &[],
@@ -935,7 +958,35 @@ impl HostFn {
Self::PlotPoints => &["path", "grid"],
Self::PlotConic => &["b", "h", "k", "path", "grid"],
Self::ForeignSetCmd => &["args"],
Self::ForeignRun | Self::ForeignRunLang => &["stdin"],
Self::ForeignRun | Self::ForeignRunLang => {
&["stdin", "env", "cwd", "timeout", "args", "max_output"]
}
Self::ForeignInvoke => &[
"code",
"lang",
"source",
"stdin",
"input",
"env",
"cwd",
"timeout",
"args",
"output",
"max_output",
"cmd",
"keep",
],
Self::ForeignProbe => &["lang"],
Self::ForeignRegister => &[
"cmd",
"args",
"template",
"ext",
"env",
"version_args",
],
Self::ForeignUnset => &[],
Self::ForeignLoadConfig => &["path"],
Self::WritebackRecord | Self::WritebackGet | Self::WritebackClear => {
&["at_end", "key", "line"]
}
@@ -1184,7 +1235,12 @@ pub fn call_host(
HostFn::ForeignRun => {
#[cfg(feature = "exec-host")]
{
foreign::run(ctx, require(bound, "code")?, bound.get("stdin"))
foreign::run_with_options(
ctx,
require(bound, "code")?,
bound.get("stdin"),
bound,
)
}
#[cfg(not(feature = "exec-host"))]
{
@@ -1195,11 +1251,12 @@ pub fn call_host(
HostFn::ForeignRunLang => {
#[cfg(feature = "exec-host")]
{
foreign::run_lang(
foreign::run_lang_with_options(
ctx,
require(bound, "lang")?,
require(bound, "source")?,
bound.get("stdin"),
bound,
)
}
#[cfg(not(feature = "exec-host"))]
@@ -1208,6 +1265,87 @@ pub fn call_host(
Err("foreign unavailable in browser wasm".into())
}
}
HostFn::ForeignInvoke => {
#[cfg(feature = "exec-host")]
{
foreign::invoke(
ctx,
foreign::InvokeArgs {
code: bound.get("code"),
lang: bound.get("lang"),
source: bound.get("source"),
stdin: bound.get("stdin"),
input: bound.get("input"),
env: bound.get("env"),
cwd: bound.get("cwd"),
timeout: bound.get("timeout"),
args: bound.get("args"),
output: bound.get("output"),
max_output: bound.get("max_output"),
cmd: bound.get("cmd"),
keep: bound.get("keep"),
},
)
}
#[cfg(not(feature = "exec-host"))]
{
let _ = (ctx, bound);
Err("foreign unavailable in browser wasm".into())
}
}
HostFn::ForeignProbe => {
#[cfg(feature = "exec-host")]
{
foreign::probe(ctx, bound.get("lang"))
}
#[cfg(not(feature = "exec-host"))]
{
let _ = (ctx, bound);
Err("foreign unavailable in browser wasm".into())
}
}
HostFn::ForeignRegister => {
#[cfg(feature = "exec-host")]
{
foreign::register(
ctx,
require(bound, "lang")?,
bound.get("cmd"),
bound.get("args"),
bound.get("template"),
bound.get("ext"),
bound.get("env"),
bound.get("version_args"),
)
}
#[cfg(not(feature = "exec-host"))]
{
let _ = (ctx, bound);
Err("foreign unavailable in browser wasm".into())
}
}
HostFn::ForeignUnset => {
#[cfg(feature = "exec-host")]
{
foreign::unset(ctx, require(bound, "lang")?)
}
#[cfg(not(feature = "exec-host"))]
{
let _ = (ctx, bound);
Err("foreign unavailable in browser wasm".into())
}
}
HostFn::ForeignLoadConfig => {
#[cfg(feature = "exec-host")]
{
foreign::load_config(ctx, bound.get("path"))
}
#[cfg(not(feature = "exec-host"))]
{
let _ = (ctx, bound);
Err("foreign unavailable in browser wasm".into())
}
}
HostFn::ForeignLangs => {
#[cfg(feature = "exec-host")]
{
+1468 -224
View File
File diff suppressed because it is too large. Load diff
+10 -1
View File
@@ -27,7 +27,7 @@ mod secrets;
mod stats;
mod text_ops;
mod time;
mod toml_ops;
pub(crate) mod toml_ops;
mod url_ops;
mod uuid_ops;
#[cfg(feature = "plugin-host")]
@@ -93,6 +93,12 @@ pub struct HostContext {
pub plots: Vec<PlotArtifact>,
/// lang → interpreter argv (from `set_cmd`).
pub foreign_cmds: HashMap<String, Vec<String>>,
/// Process-local language registrations (`register` / `set_cmd`).
pub foreign_specs: HashMap<String, crate::foreign::registry::LangSpec>,
/// Project config specs (`.marqdo/foreign.toml`), lower priority than runtime.
pub foreign_config_specs: HashMap<String, crate::foreign::registry::LangSpec>,
/// Whether project config has been discovered/loaded.
pub foreign_config_loaded: bool,
/// Loaded native plugins (ABI v1/v2).
pub plugins: PluginState,
/// Entry `.mq.md` source text (for ABI host_query / context injection).
@@ -128,6 +134,9 @@ impl Default for HostContext {
rng: 0xC0FF_EE42_CAFE_BABE,
plots: Vec::new(),
foreign_cmds: HashMap::new(),
foreign_specs: HashMap::new(),
foreign_config_specs: HashMap::new(),
foreign_config_loaded: false,
plugins: PluginState::default(),
entry_source: None,
entry_path: None,
+13 -7
View File
@@ -120,8 +120,8 @@ fn republish_child_event(line: &str) {
pub(crate) struct ForeignTask {
child: Child,
temp_script: PathBuf,
stdout: JoinHandle<String>,
stderr: JoinHandle<String>,
stdout: JoinHandle<(String, bool)>,
stderr: JoinHandle<(String, bool)>,
}
enum FnState {
@@ -392,6 +392,9 @@ fn spawn_fn(
let cwd = ctx.cwd.clone();
let caps = ctx.caps.clone();
let foreign_cmds = ctx.foreign_cmds.clone();
let foreign_specs = ctx.foreign_specs.clone();
let foreign_config_specs = ctx.foreign_config_specs.clone();
let foreign_config_loaded = ctx.foreign_config_loaded;
let fs_root = ctx.fs_root.clone();
let argv = ctx.argv.clone();
@@ -407,6 +410,9 @@ fn spawn_fn(
host.fs_root = fs_root;
host.argv = argv;
host.foreign_cmds = foreign_cmds;
host.foreign_specs = foreign_specs;
host.foreign_config_specs = foreign_config_specs;
host.foreign_config_loaded = foreign_config_loaded;
host.set_entry_source(Some(&module_path), &source);
host.entry_metadata = module.metadata.clone();
let mut interp = Interpreter::with_capture(Some(&module_path), false).with_host(host);
@@ -587,12 +593,9 @@ fn join_foreign(mut task: ForeignTask) -> Result<Value, String> {
.child
.wait()
.map_err(|e| format!("subtask foreign join: {e}"))?;
let stdout = task.stdout.join().unwrap_or_default();
let stderr = task.stderr.join().unwrap_or_default();
let (stdout, out_trunc) = task.stdout.join().unwrap_or_default();
let (stderr, err_trunc) = task.stderr.join().unwrap_or_default();
let _ = std::fs::remove_file(&task.temp_script);
if stdout.len() > foreign::MAX_OUTPUT || stderr.len() > foreign::MAX_OUTPUT {
return Err("subtask foreign output too large".into());
}
if !status.success() {
let code = status.code().unwrap_or(1);
let mut msg = format!("subtask foreign failed (exit={code})");
@@ -609,6 +612,9 @@ fn join_foreign(mut task: ForeignTask) -> Result<Value, String> {
out.pop();
}
}
if out_trunc || err_trunc {
out.push_str("\n…(truncated)");
}
Ok(Value::Text(out))
}