feat(foreign): JSON call and parallel fan-out
F2: call/call_many JSON bridge, run_many ordered parallel execution with per-item isolation and concurrency cap, plus node gold tests for call and call_many.
This commit is contained in:
1 parent
e32ff8f6b3
commit
7859170aec
8 files changed
+597
-19
No files matched your search
+20
-11
@@ -124,9 +124,10 @@ pub fn canonical_param(builtin: &str, param: &str) -> String {
|
||||
("host_json_stringify" | "stringify", "indent" | "缩进") => "indent".into(),
|
||||
("host_plot", "steps" | "步数") => "steps".into(),
|
||||
("host_plot", "derivative" | "导数") => "derivative".into(),
|
||||
("host_foreign_run" | "host_foreign_invoke", "code" | "代码") => "code".into(),
|
||||
("host_foreign_run" | "host_foreign_invoke" | "host_foreign_call" | "host_foreign_call_many", "code" | "代码") => "code".into(),
|
||||
(
|
||||
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke",
|
||||
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke"
|
||||
| "host_foreign_call",
|
||||
"stdin" | "标准输入",
|
||||
) => "stdin".into(),
|
||||
(
|
||||
@@ -135,42 +136,50 @@ pub fn canonical_param(builtin: &str, param: &str) -> String {
|
||||
| "host_foreign_invoke"
|
||||
| "host_foreign_probe"
|
||||
| "host_foreign_register"
|
||||
| "host_foreign_unset",
|
||||
| "host_foreign_unset"
|
||||
| "host_foreign_call"
|
||||
| "host_foreign_call_many",
|
||||
"lang" | "语言",
|
||||
) => "lang".into(),
|
||||
(
|
||||
"host_foreign_set_cmd" | "host_foreign_invoke" | "host_foreign_register",
|
||||
"host_foreign_set_cmd"
|
||||
| "host_foreign_invoke"
|
||||
| "host_foreign_register"
|
||||
| "host_foreign_call_many",
|
||||
"cmd" | "命令",
|
||||
) => "cmd".into(),
|
||||
("host_foreign_run_lang" | "host_foreign_invoke", "source" | "源码") => "source".into(),
|
||||
("host_foreign_run_lang" | "host_foreign_invoke" | "host_foreign_call" | "host_foreign_call_many", "source" | "源码") => "source".into(),
|
||||
(
|
||||
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke",
|
||||
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke" | "host_foreign_call" | "host_foreign_call_many",
|
||||
"env" | "环境",
|
||||
) => "env".into(),
|
||||
(
|
||||
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke",
|
||||
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke" | "host_foreign_call" | "host_foreign_call_many",
|
||||
"cwd" | "工作目录",
|
||||
) => "cwd".into(),
|
||||
(
|
||||
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke",
|
||||
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke" | "host_foreign_call" | "host_foreign_call_many",
|
||||
"timeout" | "超时",
|
||||
) => "timeout".into(),
|
||||
(
|
||||
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke"
|
||||
| "host_foreign_register",
|
||||
| "host_foreign_register" | "host_foreign_call" | "host_foreign_call_many",
|
||||
"args" | "参数",
|
||||
) => "args".into(),
|
||||
(
|
||||
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke",
|
||||
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke" | "host_foreign_call_many",
|
||||
"max_output" | "最大输出",
|
||||
) => "max_output".into(),
|
||||
("host_foreign_invoke", "input" | "输入") => "input".into(),
|
||||
("host_foreign_invoke" | "host_foreign_call", "input" | "输入") => "input".into(),
|
||||
("host_foreign_invoke", "output" | "输出") => "output".into(),
|
||||
("host_foreign_invoke", "keep" | "保留") => "keep".into(),
|
||||
("host_foreign_register", "template" | "模板") => "template".into(),
|
||||
("host_foreign_register", "ext" | "扩展名") => "ext".into(),
|
||||
("host_foreign_register", "version_args" | "版本参数") => "version_args".into(),
|
||||
("host_foreign_load_config", "path" | "路径") => "path".into(),
|
||||
("host_foreign_run_many", "runs" | "任务表") => "runs".into(),
|
||||
("host_foreign_call_many", "inputs" | "输入表") => "inputs".into(),
|
||||
("host_foreign_run_many" | "host_foreign_call_many", "concurrency" | "并发") => "concurrency".into(),
|
||||
("host_ext_native_path", "name" | "名") => "name".into(),
|
||||
_ => param.to_string(),
|
||||
}
|
||||
|
||||
+104
-1
@@ -207,6 +207,9 @@ pub enum HostFn {
|
||||
ForeignRegister = 198,
|
||||
ForeignUnset = 199,
|
||||
ForeignLoadConfig = 200,
|
||||
ForeignCall = 201,
|
||||
ForeignRunMany = 202,
|
||||
ForeignCallMany = 203,
|
||||
}
|
||||
|
||||
|
||||
@@ -404,6 +407,9 @@ impl HostFn {
|
||||
198 => Self::ForeignRegister,
|
||||
199 => Self::ForeignUnset,
|
||||
200 => Self::ForeignLoadConfig,
|
||||
201 => Self::ForeignCall,
|
||||
202 => Self::ForeignRunMany,
|
||||
203 => Self::ForeignCallMany,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -485,6 +491,9 @@ impl HostFn {
|
||||
"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_call" | "foreign_call" => Self::ForeignCall,
|
||||
"host_foreign_run_many" | "foreign_run_many" => Self::ForeignRunMany,
|
||||
"host_foreign_call_many" | "foreign_call_many" => Self::ForeignCallMany,
|
||||
"host_foreign_langs" => Self::ForeignLangs,
|
||||
"host_dotenv_load" | "load_dotenv" => Self::DotenvLoad,
|
||||
"host_http_request" | "http_request" => Self::HttpRequest,
|
||||
@@ -686,6 +695,9 @@ impl HostFn {
|
||||
Self::ForeignRegister => "host_foreign_register",
|
||||
Self::ForeignUnset => "host_foreign_unset",
|
||||
Self::ForeignLoadConfig => "host_foreign_load_config",
|
||||
Self::ForeignCall => "host_foreign_call",
|
||||
Self::ForeignRunMany => "host_foreign_run_many",
|
||||
Self::ForeignCallMany => "host_foreign_call_many",
|
||||
Self::ForeignLangs => "host_foreign_langs",
|
||||
Self::DotenvLoad => "host_dotenv_load",
|
||||
Self::HttpRequest => "host_http_request",
|
||||
@@ -852,9 +864,11 @@ impl HostFn {
|
||||
Self::ForeignSetCmd => &["lang", "cmd"],
|
||||
Self::ForeignRun => &["code"],
|
||||
Self::ForeignRunLang => &["lang", "source"],
|
||||
Self::ForeignInvoke => &[],
|
||||
Self::ForeignInvoke | Self::ForeignCall => &[],
|
||||
Self::ForeignProbe | Self::ForeignLoadConfig => &[],
|
||||
Self::ForeignRegister | Self::ForeignUnset => &["lang"],
|
||||
Self::ForeignRunMany => &["runs"],
|
||||
Self::ForeignCallMany => &["inputs"],
|
||||
Self::ForeignLangs => &[],
|
||||
Self::PluginLoad => &["path"],
|
||||
Self::PluginUnload | Self::PluginList => &[],
|
||||
@@ -987,6 +1001,22 @@ impl HostFn {
|
||||
],
|
||||
Self::ForeignUnset => &[],
|
||||
Self::ForeignLoadConfig => &["path"],
|
||||
Self::ForeignCall => &[
|
||||
"code", "lang", "source", "input", "stdin", "env", "cwd", "timeout", "args",
|
||||
],
|
||||
Self::ForeignRunMany => &["concurrency"],
|
||||
Self::ForeignCallMany => &[
|
||||
"code",
|
||||
"lang",
|
||||
"source",
|
||||
"cmd",
|
||||
"env",
|
||||
"cwd",
|
||||
"timeout",
|
||||
"args",
|
||||
"max_output",
|
||||
"concurrency",
|
||||
],
|
||||
Self::WritebackRecord | Self::WritebackGet | Self::WritebackClear => {
|
||||
&["at_end", "key", "line"]
|
||||
}
|
||||
@@ -1346,6 +1376,79 @@ pub fn call_host(
|
||||
Err("foreign unavailable in browser wasm".into())
|
||||
}
|
||||
}
|
||||
HostFn::ForeignCall => {
|
||||
#[cfg(feature = "exec-host")]
|
||||
{
|
||||
foreign::call(
|
||||
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: None,
|
||||
max_output: None,
|
||||
cmd: None,
|
||||
keep: None,
|
||||
},
|
||||
)
|
||||
}
|
||||
#[cfg(not(feature = "exec-host"))]
|
||||
{
|
||||
let _ = (ctx, bound);
|
||||
Err("foreign unavailable in browser wasm".into())
|
||||
}
|
||||
}
|
||||
HostFn::ForeignRunMany => {
|
||||
#[cfg(feature = "exec-host")]
|
||||
{
|
||||
foreign::run_many(
|
||||
ctx,
|
||||
require(bound, "runs")?,
|
||||
bound.get("concurrency"),
|
||||
)
|
||||
}
|
||||
#[cfg(not(feature = "exec-host"))]
|
||||
{
|
||||
let _ = (ctx, bound);
|
||||
Err("foreign unavailable in browser wasm".into())
|
||||
}
|
||||
}
|
||||
HostFn::ForeignCallMany => {
|
||||
#[cfg(feature = "exec-host")]
|
||||
{
|
||||
foreign::call_many(
|
||||
ctx,
|
||||
foreign::InvokeArgs {
|
||||
code: bound.get("code"),
|
||||
lang: bound.get("lang"),
|
||||
source: bound.get("source"),
|
||||
stdin: None,
|
||||
input: None,
|
||||
env: bound.get("env"),
|
||||
cwd: bound.get("cwd"),
|
||||
timeout: bound.get("timeout"),
|
||||
args: bound.get("args"),
|
||||
output: None,
|
||||
max_output: bound.get("max_output"),
|
||||
cmd: bound.get("cmd"),
|
||||
keep: None,
|
||||
},
|
||||
require(bound, "inputs")?,
|
||||
bound.get("concurrency"),
|
||||
)
|
||||
}
|
||||
#[cfg(not(feature = "exec-host"))]
|
||||
{
|
||||
let _ = (ctx, bound);
|
||||
Err("foreign unavailable in browser wasm".into())
|
||||
}
|
||||
}
|
||||
HostFn::ForeignLangs => {
|
||||
#[cfg(feature = "exec-host")]
|
||||
{
|
||||
|
||||
+345
-7
@@ -3,17 +3,19 @@
|
||||
//! F0: safe one-shot execution + structured `invoke` records.
|
||||
//! Later phases add language registry, JSON bridge, parallel fan-out and sessions.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::io::{Read, Write};
|
||||
use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Child, Command, Stdio};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::foreign::registry::{self, LangSpec};
|
||||
use crate::foreign::split_cmd_line;
|
||||
use crate::host::json::{json_to_value, value_to_json};
|
||||
use crate::host::HostContext;
|
||||
use crate::host::{HostCaps, HostContext};
|
||||
use crate::value::{CodeBlock, Value};
|
||||
|
||||
const DEFAULT_TIMEOUT_SECS: f64 = 10.0;
|
||||
@@ -581,9 +583,13 @@ fn bound_job(
|
||||
}
|
||||
|
||||
/// Structured one-shot execution. Never errors for process failures; only bad args/policy.
|
||||
pub fn invoke(ctx: &mut HostContext, args: InvokeArgs<'_>) -> Result<Value, String> {
|
||||
ensure_config(ctx);
|
||||
let (lang, source) = resolve_invoke_source(&args)?;
|
||||
struct PreparedInvoke {
|
||||
job: Job,
|
||||
output: String,
|
||||
}
|
||||
|
||||
fn prepare_invoke(args: &InvokeArgs<'_>) -> Result<PreparedInvoke, String> {
|
||||
let (lang, source) = resolve_invoke_source(args)?;
|
||||
let stdin = opt_text(args.stdin)?;
|
||||
let input_json = match args.input {
|
||||
None | Some(Value::None) => None,
|
||||
@@ -612,11 +618,38 @@ pub fn invoke(ctx: &mut HostContext, args: InvokeArgs<'_>) -> Result<Value, Stri
|
||||
job.max_output = parse_max_output(args.max_output)?;
|
||||
job.cmd_override = opt_text(args.cmd)?;
|
||||
job.keep = parse_bool(args.keep, "keep", false)?;
|
||||
let exec = execute_job(ctx, &job)?;
|
||||
let (result, result_error) = parse_output(&exec.stdout, &output);
|
||||
Ok(PreparedInvoke { job, output })
|
||||
}
|
||||
|
||||
pub fn invoke(ctx: &mut HostContext, args: InvokeArgs<'_>) -> Result<Value, String> {
|
||||
ensure_config(ctx);
|
||||
let prepared = prepare_invoke(&args)?;
|
||||
let exec = execute_job(ctx, &prepared.job)?;
|
||||
let (result, result_error) = parse_output(&exec.stdout, &prepared.output);
|
||||
Ok(exec.record(result, result_error))
|
||||
}
|
||||
|
||||
/// JSON-in / JSON-out convenience: raises on process failure or invalid JSON.
|
||||
pub fn call(ctx: &mut HostContext, args: InvokeArgs<'_>) -> Result<Value, String> {
|
||||
ensure_config(ctx);
|
||||
let mut prepared = prepare_invoke(&args)?;
|
||||
prepared.output = "json".into();
|
||||
let exec = execute_job(ctx, &prepared.job)?;
|
||||
if !exec.ok {
|
||||
return Err(format_failure(&exec));
|
||||
}
|
||||
if exec.stdout.trim().is_empty() {
|
||||
return Ok(Value::None);
|
||||
}
|
||||
match serde_json::from_str::<serde_json::Value>(&exec.stdout) {
|
||||
Ok(j) => json_to_value(&j),
|
||||
Err(e) => Err(format!(
|
||||
"foreign call: stdout is not JSON: {e}; stdout: {}",
|
||||
tail(exec.stdout.trim(), 512)
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn present(v: Option<&Value>) -> Option<&Value> {
|
||||
match v {
|
||||
None | Some(Value::None) => None,
|
||||
@@ -624,6 +657,273 @@ fn present(v: Option<&Value>) -> Option<&Value> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ForeignEnv {
|
||||
cwd: PathBuf,
|
||||
fs_root: Option<PathBuf>,
|
||||
entry_path: Option<PathBuf>,
|
||||
caps: HostCaps,
|
||||
foreign_cmds: HashMap<String, Vec<String>>,
|
||||
foreign_specs: HashMap<String, LangSpec>,
|
||||
foreign_config_specs: HashMap<String, LangSpec>,
|
||||
foreign_config_loaded: bool,
|
||||
sandbox_session: Option<String>,
|
||||
}
|
||||
|
||||
impl ForeignEnv {
|
||||
fn from_ctx(ctx: &HostContext) -> Self {
|
||||
Self {
|
||||
cwd: ctx.cwd.clone(),
|
||||
fs_root: ctx.fs_root.clone(),
|
||||
entry_path: ctx.entry_path.clone(),
|
||||
caps: ctx.caps.clone(),
|
||||
foreign_cmds: ctx.foreign_cmds.clone(),
|
||||
foreign_specs: ctx.foreign_specs.clone(),
|
||||
foreign_config_specs: ctx.foreign_config_specs.clone(),
|
||||
foreign_config_loaded: ctx.foreign_config_loaded,
|
||||
sandbox_session: ctx.sandbox_session.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn to_ctx(&self) -> HostContext {
|
||||
let mut c = HostContext::default();
|
||||
c.cwd = self.cwd.clone();
|
||||
c.fs_root = self.fs_root.clone();
|
||||
c.entry_path = self.entry_path.clone();
|
||||
c.caps = self.caps.clone();
|
||||
c.foreign_cmds = self.foreign_cmds.clone();
|
||||
c.foreign_specs = self.foreign_specs.clone();
|
||||
c.foreign_config_specs = self.foreign_config_specs.clone();
|
||||
c.foreign_config_loaded = self.foreign_config_loaded;
|
||||
c.sandbox_session = self.sandbox_session.clone();
|
||||
c
|
||||
}
|
||||
}
|
||||
|
||||
struct PreparedMany {
|
||||
idx: usize,
|
||||
job: Job,
|
||||
output: String,
|
||||
}
|
||||
|
||||
fn parse_concurrency(v: Option<&Value>) -> Result<usize, String> {
|
||||
let n = match v {
|
||||
None | Some(Value::None) => 4,
|
||||
Some(Value::Int(n)) if *n >= 0 => *n as usize,
|
||||
Some(_) => return Err("concurrency must be a non-negative integer".into()),
|
||||
};
|
||||
Ok(n.min(32))
|
||||
}
|
||||
|
||||
fn map_get<'a>(m: &'a Value, key: &str) -> Option<&'a Value> {
|
||||
match m {
|
||||
Value::Map(entries) => entries
|
||||
.iter()
|
||||
.find(|(k, _)| k == key)
|
||||
.map(|(_, v)| v)
|
||||
.filter(|v| !matches!(v, Value::None)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn args_from_map<'a>(m: &'a Value) -> InvokeArgs<'a> {
|
||||
InvokeArgs {
|
||||
code: map_get(m, "code"),
|
||||
lang: map_get(m, "lang"),
|
||||
source: map_get(m, "source"),
|
||||
stdin: map_get(m, "stdin"),
|
||||
input: map_get(m, "input"),
|
||||
env: map_get(m, "env"),
|
||||
cwd: map_get(m, "cwd"),
|
||||
timeout: map_get(m, "timeout"),
|
||||
args: map_get(m, "args"),
|
||||
output: map_get(m, "output"),
|
||||
max_output: map_get(m, "max_output"),
|
||||
cmd: map_get(m, "cmd"),
|
||||
keep: map_get(m, "keep"),
|
||||
}
|
||||
}
|
||||
|
||||
fn error_record(ctx: &HostContext, job: &Job, error: String) -> Value {
|
||||
Execution {
|
||||
ok: false,
|
||||
code: -1,
|
||||
stdout: String::new(),
|
||||
stderr: String::new(),
|
||||
ms: 0,
|
||||
timed_out: false,
|
||||
truncated: false,
|
||||
error,
|
||||
lang: job.lang.clone(),
|
||||
cmd: display_for_job(ctx, job, None),
|
||||
script: String::new(),
|
||||
}
|
||||
.record(Value::None, String::new())
|
||||
}
|
||||
|
||||
fn run_prepared(env: &ForeignEnv, items: Vec<PreparedMany>, concurrency: usize) -> Vec<(usize, Value)> {
|
||||
if items.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
let workers = concurrency.max(1).min(items.len());
|
||||
let queue = Arc::new(Mutex::new(VecDeque::from(items)));
|
||||
let results: Arc<Mutex<Vec<(usize, Value)>>> = Arc::new(Mutex::new(Vec::new()));
|
||||
thread::scope(|scope| {
|
||||
for _ in 0..workers {
|
||||
let queue = Arc::clone(&queue);
|
||||
let results = Arc::clone(&results);
|
||||
let env = env.clone();
|
||||
scope.spawn(move || loop {
|
||||
let item = {
|
||||
let mut q = match queue.lock() {
|
||||
Ok(q) => q,
|
||||
Err(_) => break,
|
||||
};
|
||||
q.pop_front()
|
||||
};
|
||||
let Some(item) = item else { break };
|
||||
let ctx = env.to_ctx();
|
||||
let value = match catch_unwind(AssertUnwindSafe(|| {
|
||||
execute_job(&ctx, &item.job)
|
||||
})) {
|
||||
Ok(Ok(exec)) => {
|
||||
let (result, result_error) = parse_output(&exec.stdout, &item.output);
|
||||
exec.record(result, result_error)
|
||||
}
|
||||
Ok(Err(e)) => error_record(&ctx, &item.job, e),
|
||||
Err(_) => error_record(&ctx, &item.job, "foreign worker panicked".into()),
|
||||
};
|
||||
if let Ok(mut out) = results.lock() {
|
||||
out.push((item.idx, value));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
let mut out = match Arc::try_unwrap(results) {
|
||||
Ok(m) => m.into_inner().unwrap_or_default(),
|
||||
Err(shared) => shared.lock().map(|m| m.clone()).unwrap_or_default(),
|
||||
};
|
||||
out.sort_by_key(|(i, _)| *i);
|
||||
out
|
||||
}
|
||||
|
||||
/// Build a prepared job for one `runs` item (code value or option map).
|
||||
fn prepare_many_item(ctx: &HostContext, idx: usize, item: &Value) -> Result<PreparedMany, String> {
|
||||
match item {
|
||||
Value::Code(c) => Ok(PreparedMany {
|
||||
idx,
|
||||
job: Job::simple(&c.lang, &c.source, None),
|
||||
output: "auto".into(),
|
||||
}),
|
||||
Value::Map(_) => {
|
||||
let args = args_from_map(item);
|
||||
let prepared = prepare_invoke(&args)?;
|
||||
let _ = ctx;
|
||||
Ok(PreparedMany {
|
||||
idx,
|
||||
job: prepared.job,
|
||||
output: prepared.output,
|
||||
})
|
||||
}
|
||||
_ => Err(format!(
|
||||
"runs[{idx}] must be a code value or a map of invoke options"
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_many(
|
||||
ctx: &mut HostContext,
|
||||
runs: &Value,
|
||||
concurrency: Option<&Value>,
|
||||
) -> Result<Value, String> {
|
||||
ensure_config(ctx);
|
||||
if !ctx.allow_exec() {
|
||||
return Err("foreign run_many denied by host policy".into());
|
||||
}
|
||||
let list = match runs {
|
||||
Value::List(xs) => xs,
|
||||
_ => return Err("run_many runs must be a list".into()),
|
||||
};
|
||||
let mut prepared = Vec::with_capacity(list.len());
|
||||
for (idx, item) in list.iter().enumerate() {
|
||||
prepared.push(prepare_many_item(ctx, idx, item)?);
|
||||
}
|
||||
let n = prepared.len();
|
||||
let requested = parse_concurrency(concurrency)?;
|
||||
let concurrency = if requested == 0 {
|
||||
n.min(8)
|
||||
} else {
|
||||
requested
|
||||
};
|
||||
let env = ForeignEnv::from_ctx(ctx);
|
||||
let results = run_prepared(&env, prepared, concurrency);
|
||||
Ok(Value::List(results.into_iter().map(|(_, v)| v).collect()))
|
||||
}
|
||||
|
||||
pub fn call_many(
|
||||
ctx: &mut HostContext,
|
||||
args: InvokeArgs<'_>,
|
||||
inputs: &Value,
|
||||
concurrency: Option<&Value>,
|
||||
) -> Result<Value, String> {
|
||||
ensure_config(ctx);
|
||||
if !ctx.allow_exec() {
|
||||
return Err("foreign call_many denied by host policy".into());
|
||||
}
|
||||
let list = match inputs {
|
||||
Value::List(xs) => xs,
|
||||
_ => return Err("call_many inputs must be a list".into()),
|
||||
};
|
||||
let base = prepare_invoke(&args)?;
|
||||
let n = list.len();
|
||||
let requested = parse_concurrency(concurrency)?;
|
||||
let concurrency = if requested == 0 {
|
||||
n.min(8)
|
||||
} else {
|
||||
requested
|
||||
};
|
||||
let mut prepared = Vec::with_capacity(n);
|
||||
for (idx, input) in list.iter().enumerate() {
|
||||
let json = serde_json::to_string(&value_to_json(input)?)
|
||||
.map_err(|e| format!("call_many input JSON encode: {e}"))?;
|
||||
let mut job = base.job.clone();
|
||||
job.stdin = Some(json.clone());
|
||||
job.input_json = Some(json);
|
||||
prepared.push(PreparedMany {
|
||||
idx,
|
||||
job,
|
||||
output: "json".into(),
|
||||
});
|
||||
}
|
||||
let env = ForeignEnv::from_ctx(ctx);
|
||||
let records = run_prepared(&env, prepared, concurrency);
|
||||
let mut results = Vec::with_capacity(records.len());
|
||||
let mut errors = Vec::new();
|
||||
for (idx, record) in records {
|
||||
let ok = matches!(map_get(&record, "ok"), Some(Value::Bool(true)));
|
||||
let result_error = map_get(&record, "result_error")
|
||||
.and_then(|v| v.reveal_text())
|
||||
.unwrap_or("");
|
||||
if !ok {
|
||||
let err = map_get(&record, "error")
|
||||
.and_then(|v| v.reveal_text())
|
||||
.or_else(|| map_get(&record, "stderr").and_then(|v| v.reveal_text()))
|
||||
.unwrap_or("foreign call failed");
|
||||
errors.push(format!("call_many[{idx}]: {err}"));
|
||||
continue;
|
||||
}
|
||||
if !result_error.is_empty() {
|
||||
errors.push(format!("call_many[{idx}]: {result_error}"));
|
||||
continue;
|
||||
}
|
||||
results.push(map_get(&record, "result").cloned().unwrap_or(Value::None));
|
||||
}
|
||||
if !errors.is_empty() {
|
||||
return Err(errors.join("; "));
|
||||
}
|
||||
Ok(Value::List(results))
|
||||
}
|
||||
|
||||
fn resolve_invoke_source(args: &InvokeArgs<'_>) -> Result<(String, String), String> {
|
||||
if let Some(code) = present(args.code) {
|
||||
if present(args.lang).is_some() || present(args.source).is_some() {
|
||||
@@ -1625,4 +1925,42 @@ mod tests {
|
||||
assert_eq!(text, "0123");
|
||||
assert!(truncated);
|
||||
}
|
||||
|
||||
fn node_available() -> bool {
|
||||
Command::new("node")
|
||||
.args(["-e", "process.exit(0)"])
|
||||
.output()
|
||||
.map(|o| o.status.success())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_many_keeps_order_and_isolates_failures() {
|
||||
if !node_available() {
|
||||
return;
|
||||
}
|
||||
let mut ctx = HostContext::default();
|
||||
let runs = Value::List(vec![
|
||||
Value::Code(CodeBlock {
|
||||
lang: "node".into(),
|
||||
source: "console.log('a')".into(),
|
||||
}),
|
||||
Value::Code(CodeBlock {
|
||||
lang: "node".into(),
|
||||
source: "process.exit(2)".into(),
|
||||
}),
|
||||
Value::Code(CodeBlock {
|
||||
lang: "node".into(),
|
||||
source: "console.log('c')".into(),
|
||||
}),
|
||||
]);
|
||||
let out = run_many(&mut ctx, &runs, Some(&Value::Int(2))).unwrap();
|
||||
let Value::List(records) = out else {
|
||||
panic!("expected list");
|
||||
};
|
||||
assert_eq!(records.len(), 3);
|
||||
assert_eq!(map_get(&records[0], "stdout").and_then(|v| v.reveal_text()), Some("a\n"));
|
||||
assert_eq!(map_get(&records[1], "ok"), Some(&Value::Bool(false)));
|
||||
assert_eq!(map_get(&records[2], "stdout").and_then(|v| v.reveal_text()), Some("c\n"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user