Files
marqdo/src/host/subtask.rs
T

825 lines
28 KiB
Rust

//! Concurrent subtasks: OS file children, in-process functions, foreign subprocesses.
//! Parent `HostContext` drop kills/waits file & foreign children; function threads detach.
//!
//! Quiet file children get `MARQDO_EVENT_FORWARD` so LLM stream events published in the
//! child process are tailed into the parent EventBus while `wait` blocks (view SSE).
use crate::host::as_text;
use std::collections::HashMap;
use std::io::{Read, Seek, SeekFrom};
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};
use std::time::Duration;
/// Cap for file-child stdout/stderr returned on `wait` (parent observation).
const FILE_IO_CAP: usize = 8 * 1024;
use crate::host::foreign;
use crate::host::HostContext;
use crate::interp::Interpreter;
use crate::load::load_module;
use crate::value::{CodeBlock, Value};
/// Stored subtask handle (tagged by spawn kind).
pub(crate) enum Handle {
File(FileTask),
Foreign(ForeignTask),
Function(FunctionTask),
}
impl std::fmt::Debug for Handle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Handle::File(_) => f.write_str("Subtask::File"),
Handle::Foreign(_) => f.write_str("Subtask::Foreign"),
Handle::Function(_) => f.write_str("Subtask::Function"),
}
}
}
pub(crate) struct FileTask {
child: Child,
result_path: PathBuf,
/// NDJSON path the child appends EventBus frames to (`MARQDO_EVENT_FORWARD`).
events_path: Option<PathBuf>,
/// Stops the parent-side tailer that republishes child events.
forward_stop: Option<Arc<AtomicBool>>,
forward: Option<JoinHandle<()>>,
/// Present when `quiet=True` (piped capture). Absent when inheriting TTY.
stdout: Option<JoinHandle<String>>,
stderr: Option<JoinHandle<String>>,
}
fn stop_event_forward(task: &mut FileTask) {
if let Some(stop) = task.forward_stop.take() {
stop.store(true, Ordering::SeqCst);
}
if let Some(h) = task.forward.take() {
let _ = h.join();
}
if let Some(p) = task.events_path.take() {
let _ = std::fs::remove_file(p);
}
}
/// Tail `MARQDO_EVENT_FORWARD` NDJSON and republish onto the parent bus with `source=child`.
fn spawn_event_forwarder(path: PathBuf, stop: Arc<AtomicBool>) -> JoinHandle<()> {
thread::spawn(move || {
let mut pos: u64 = 0;
let mut carry = String::new();
loop {
let done = stop.load(Ordering::SeqCst);
if let Ok(mut f) = std::fs::File::open(&path) {
if f.seek(SeekFrom::Start(pos)).is_ok() {
let mut buf = String::new();
if f.read_to_string(&mut buf).is_ok() && !buf.is_empty() {
pos += buf.len() as u64;
carry.push_str(&buf);
while let Some(idx) = carry.find('\n') {
let line = carry[..idx].trim().to_string();
carry.drain(..=idx);
if line.is_empty() {
continue;
}
republish_child_event(&line);
}
}
}
}
if done {
if !carry.is_empty() {
let line = carry.trim();
if !line.is_empty() {
republish_child_event(line);
}
carry.clear();
}
break;
}
thread::sleep(Duration::from_millis(16));
}
})
}
fn republish_child_event(line: &str) {
let Ok(mut v) = serde_json::from_str::<serde_json::Value>(line) else {
return;
};
// Skip nested terminal frames — parent owns run lifecycle.
if v.get("type").and_then(|t| t.as_str()) == Some("done") && v.get("run_id").is_none() {
return;
}
if let Some(obj) = v.as_object_mut() {
obj.insert("source".into(), serde_json::json!("child"));
}
crate::host::event_bus::EventBus::global().publish_json(&v.to_string());
}
pub(crate) struct ForeignTask {
child: Child,
temp_script: PathBuf,
stdout: JoinHandle<(String, bool)>,
stderr: JoinHandle<(String, bool)>,
}
enum FnState {
Running,
Done(Value),
Failed(String),
Killed,
}
pub(crate) struct FunctionTask {
state: Arc<Mutex<FnState>>,
handle: Option<JoinHandle<()>>,
}
fn as_code(v: &Value) -> Result<&CodeBlock, String> {
match v {
Value::Code(c) => Ok(c),
_ => Err("code must be a bound ```lang fence value".into()),
}
}
fn opt_text(v: Option<&Value>) -> Result<Option<&str>, String> {
match v {
None | Some(Value::None) => Ok(None),
Some(v) => Ok(Some(as_text(v, "value")?)),
}
}
fn is_set(v: Option<&Value>) -> bool {
match v {
None => false,
Some(Value::None) => false,
Some(Value::Text(s)) if s.is_empty() => false,
Some(_) => true,
}
}
fn child_id(id: &Value) -> Result<u64, String> {
match id {
Value::Int(n) if *n > 0 => Ok(*n as u64),
_ => Err("subtask id must be positive int".into()),
}
}
fn next_id(ctx: &mut HostContext) -> u64 {
ctx.subtask_seq = ctx.subtask_seq.wrapping_add(1);
ctx.subtask_seq
}
fn status_map(status: &str, extra: Vec<(String, Value)>) -> Value {
let mut pairs = vec![("status".into(), Value::Text(status.into()))];
pairs.extend(extra);
Value::Map(pairs)
}
fn fn_args_from_value(args: Option<&Value>) -> Result<Vec<(String, Value)>, String> {
let Some(args) = args else {
return Ok(Vec::new());
};
match args {
Value::None => Ok(Vec::new()),
Value::Map(pairs) => Ok(pairs.clone()),
Value::List(items) => {
let mut out = Vec::new();
for (i, item) in items.iter().enumerate() {
out.push((format!("arg{i}"), item.clone()));
}
Ok(out)
}
other => Err(format!(
"fn args must be map or list, got {}",
other.as_display()
)),
}
}
pub fn kill_all(ctx: &mut HostContext) {
for (_, handle) in ctx.subtasks.drain() {
match handle {
Handle::File(mut task) => {
let _ = task.child.kill();
let _ = task.child.wait();
stop_event_forward(&mut task);
if let Some(h) = task.stdout.take() {
let _ = h.join();
}
if let Some(h) = task.stderr.take() {
let _ = h.join();
}
let _ = std::fs::remove_file(&task.result_path);
}
Handle::Foreign(mut task) => {
let _ = task.child.kill();
let _ = task.child.wait();
let _ = std::fs::remove_file(&task.temp_script);
let _ = task.stdout.join();
let _ = task.stderr.join();
}
Handle::Function(mut task) => {
// Join so fire-and-forget writers (e.g. agent writeback) finish before exit.
if let Some(h) = task.handle.take() {
let _ = h.join();
}
}
}
}
}
pub fn spawn(ctx: &mut HostContext, bound: &HashMap<String, Value>) -> Result<Value, String> {
let has_path = is_set(bound.get("path"));
let has_fn = is_set(bound.get("fn"));
let has_code = is_set(bound.get("code"));
let has_lang = is_set(bound.get("lang")) && is_set(bound.get("source"));
let kinds = [has_path, has_fn, has_code, has_lang]
.into_iter()
.filter(|b| *b)
.count();
if kinds != 1 {
return Err(
"spawn: specify exactly one of path=, fn=, code=, or lang=+source=".into(),
);
}
if has_path {
spawn_file(
ctx,
require(bound, "path")?,
bound.get("args"),
bound.get("quiet"),
)
} else if has_fn {
spawn_fn(ctx, require(bound, "fn")?, bound.get("args"))
} else if has_code {
spawn_code(ctx, require(bound, "code")?, bound.get("stdin"))
} else {
spawn_lang(
ctx,
require(bound, "lang")?,
require(bound, "source")?,
bound.get("stdin"),
)
}
}
fn spawn_file(
ctx: &mut HostContext,
path: &Value,
args: Option<&Value>,
quiet: Option<&Value>,
) -> Result<Value, String> {
if !ctx.caps.exec {
return Err("subtask spawn disabled (exec capability off)".into());
}
let rel = as_text(path, "path")?;
let joined = if std::path::Path::new(rel).is_absolute() {
std::path::PathBuf::from(rel)
} else {
ctx.cwd.join(rel)
};
// Absolutize so the child `for_run` cwd/parent resolution does not double-prefix
// a relative path like `tests/ext/.marqdo/...` when current_dir is already `tests/ext`.
let file = std::fs::canonicalize(&joined).unwrap_or_else(|_| {
if joined.is_absolute() {
joined.clone()
} else {
std::env::current_dir()
.unwrap_or_else(|_| std::path::PathBuf::from("."))
.join(&joined)
}
});
let exe = std::env::current_exe().map_err(|e| format!("subtask: current_exe: {e}"))?;
let id = next_id(ctx);
let result_path = std::env::temp_dir().join(format!(
"marqdo-subtask-{}-{}.json",
std::process::id(),
id
));
let mut cmd = Command::new(&exe);
cmd.arg("run").arg(&file);
if !ctx.caps.fs_write { cmd.arg("--deny-fs-write"); }
if !ctx.caps.exec { cmd.arg("--deny-exec"); }
if !ctx.caps.net { cmd.arg("--deny-net"); }
if !ctx.caps.plugin { cmd.arg("--deny-plugin"); }
if let Some(root) = &ctx.fs_root { cmd.arg("--fs-root").arg(root); }
cmd.arg("--emit-result").arg(&result_path);
cmd.current_dir(&ctx.cwd);
cmd.stdin(Stdio::null());
let quiet = match quiet {
None | Some(Value::None) => true,
Some(v) => v.truthy(),
};
// quiet: pipe-capture for parent observation (no TTY noise).
// !quiet: inherit parent TTY; wait map omits stdout/stderr bodies.
if quiet {
cmd.stdout(Stdio::piped());
cmd.stderr(Stdio::piped());
} else {
cmd.stdout(Stdio::inherit());
cmd.stderr(Stdio::inherit());
}
if let Some(Value::List(items)) = args {
for item in items {
cmd.arg(as_text(item, "arg")?);
}
}
// Bridge child EventBus → parent while wait blocks (view SSE / concurrent progress).
let events_path = std::env::temp_dir().join(format!(
"marqdo-events-{}-{}.ndjson",
std::process::id(),
id
));
let _ = std::fs::write(&events_path, "");
cmd.env("MARQDO_EVENT_FORWARD", &events_path);
apply_run_selection_env(&mut cmd);
let mut child = cmd
.spawn()
.map_err(|e| format!("subtask spawn {}: {e}", file.display()))?;
let (stdout, stderr) = if quiet {
let mut stdout_pipe = child.stdout.take();
let mut stderr_pipe = child.stderr.take();
let stdout = thread::spawn(move || {
stdout_pipe.as_mut().map(capture_file_io).unwrap_or_default()
});
let stderr = thread::spawn(move || {
stderr_pipe.as_mut().map(capture_file_io).unwrap_or_default()
});
(Some(stdout), Some(stderr))
} else {
(None, None)
};
let forward_stop = Arc::new(AtomicBool::new(false));
let forward = spawn_event_forwarder(events_path.clone(), Arc::clone(&forward_stop));
ctx.subtasks.insert(
id,
Handle::File(FileTask {
child,
result_path,
events_path: Some(events_path),
forward_stop: Some(forward_stop),
forward: Some(forward),
stdout,
stderr,
}),
);
Ok(Value::Int(id as i64))
}
fn spawn_fn(
ctx: &mut HostContext,
name: &Value,
args: Option<&Value>,
) -> Result<Value, String> {
let name = as_text(name, "fn")?.to_string();
let call_args = fn_args_from_value(args)?;
let module_path = ctx
.entry_path
.clone()
.ok_or_else(|| "subtask fn: no entry module (run a .mq.md file first)".to_string())?;
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();
let state = Arc::new(Mutex::new(FnState::Running));
let state_bg = Arc::clone(&state);
let handle = thread::spawn(move || {
let result = (|| -> Result<Value, String> {
let module = load_module(&module_path).map_err(|e| e.to_string())?;
let source = std::fs::read_to_string(&module_path)
.map_err(|e| format!("subtask fn: read {}: {e}", module_path.display()))?;
let mut host = HostContext::for_capture(Some(&module_path), caps);
host.cwd = cwd;
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);
interp
.invoke_function(&module, &name, &call_args)
.map_err(|e| e.to_string())
})();
if let Ok(mut st) = state_bg.lock() {
if !matches!(*st, FnState::Killed) {
*st = match result {
Ok(v) => FnState::Done(v),
Err(e) => FnState::Failed(e),
};
}
}
});
let id = next_id(ctx);
ctx.subtasks.insert(
id,
Handle::Function(FunctionTask {
state,
handle: Some(handle),
}),
);
Ok(Value::Int(id as i64))
}
fn spawn_code(
ctx: &mut HostContext,
code: &Value,
stdin: Option<&Value>,
) -> Result<Value, String> {
if !ctx.caps.exec {
return Err("subtask spawn disabled (exec capability off)".into());
}
let block = as_code(code)?;
let stdin = opt_text(stdin)?;
spawn_foreign(ctx, &block.lang, &block.source, stdin)
}
fn spawn_lang(
ctx: &mut HostContext,
lang: &Value,
source: &Value,
stdin: Option<&Value>,
) -> Result<Value, String> {
if !ctx.caps.exec {
return Err("subtask spawn disabled (exec capability off)".into());
}
let lang = as_text(lang, "lang")?;
let source = as_text(source, "source")?;
let stdin = opt_text(stdin)?;
spawn_foreign(ctx, lang, source, stdin)
}
fn spawn_foreign(
ctx: &mut HostContext,
lang: &str,
source: &str,
stdin: Option<&str>,
) -> Result<Value, String> {
let spawned = foreign::spawn_source(ctx, lang, source, stdin)?;
let id = next_id(ctx);
ctx.subtasks.insert(
id,
Handle::Foreign(ForeignTask {
child: spawned.child,
temp_script: spawned.script_path,
stdout: spawned.stdout,
stderr: spawned.stderr,
}),
);
Ok(Value::Int(id as i64))
}
pub fn poll(ctx: &mut HostContext, id: &Value) -> Result<Value, String> {
let id = child_id(id)?;
let handle = ctx
.subtasks
.get_mut(&id)
.ok_or_else(|| format!("subtask: unknown id {id}"))?;
match handle {
Handle::File(task) => poll_child(&mut task.child),
Handle::Foreign(task) => poll_child(&mut task.child),
Handle::Function(task) => poll_function(&task.state),
}
}
fn poll_child(child: &mut Child) -> Result<Value, String> {
match child.try_wait().map_err(|e| format!("subtask poll: {e}"))? {
None => Ok(status_map("running", vec![])),
Some(status) => {
let code = status.code().unwrap_or(-1);
let st = if status.success() { "done" } else { "failed" };
Ok(status_map(
st,
vec![("code".into(), Value::Int(code as i64))],
))
}
}
}
fn poll_function(state: &Arc<Mutex<FnState>>) -> Result<Value, String> {
let st = state
.lock()
.map_err(|_| "subtask fn: lock poisoned".to_string())?;
match &*st {
FnState::Running => Ok(status_map("running", vec![])),
FnState::Done(v) => Ok(status_map(
"done",
vec![("value".into(), v.clone())],
)),
FnState::Failed(e) => Ok(status_map(
"failed",
vec![("error".into(), Value::Text(e.clone()))],
)),
FnState::Killed => Ok(status_map("failed", vec![("error".into(), Value::Text("killed".into()))])),
}
}
pub fn join(ctx: &mut HostContext, id: &Value) -> Result<Value, String> {
let id = child_id(id)?;
let handle = ctx
.subtasks
.remove(&id)
.ok_or_else(|| format!("subtask: unknown id {id}"))?;
match handle {
Handle::File(mut task) => {
// Forwarder runs concurrently while we block on the OS child.
let status = task
.child
.wait()
.map_err(|e| format!("subtask join: {e}"))?;
stop_event_forward(&mut task);
let code = status.code().unwrap_or(-1) as i64;
let value = read_emitted_result(&task.result_path);
let _ = std::fs::remove_file(&task.result_path);
let stdout = task
.stdout
.take()
.map(|h| h.join().unwrap_or_default())
.unwrap_or_default();
let stderr = task
.stderr
.take()
.map(|h| h.join().unwrap_or_default())
.unwrap_or_default();
let mut pairs = vec![
("code".into(), Value::Int(code)),
("value".into(), value),
];
if !stdout.is_empty() {
pairs.push(("stdout".into(), Value::Text(clip_io(&stdout))));
}
if !stderr.is_empty() {
pairs.push(("stderr".into(), Value::Text(clip_io(&stderr))));
}
Ok(Value::Map(pairs))
}
Handle::Foreign(task) => join_foreign(task),
Handle::Function(task) => join_function(task),
}
}
fn read_emitted_result(path: &PathBuf) -> Value {
match std::fs::read_to_string(path) {
Ok(text) => match serde_json::from_str::<serde_json::Value>(text.trim()) {
Ok(j) => crate::host::json::json_to_value(&j).unwrap_or(Value::None),
Err(_) => Value::None,
},
Err(_) => Value::None,
}
}
fn join_foreign(mut task: ForeignTask) -> Result<Value, String> {
let status = task
.child
.wait()
.map_err(|e| format!("subtask foreign join: {e}"))?;
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 !status.success() {
let code = status.code().unwrap_or(1);
let mut msg = format!("subtask foreign failed (exit={code})");
if !stderr.trim().is_empty() {
msg.push_str(": ");
msg.push_str(stderr.trim());
}
return Err(msg);
}
let mut out = stdout;
if out.ends_with('\n') {
out.pop();
if out.ends_with('\r') {
out.pop();
}
}
if out_trunc || err_trunc {
out.push_str("\n…(truncated)");
}
Ok(Value::Text(out))
}
fn join_function(mut task: FunctionTask) -> Result<Value, String> {
if let Some(handle) = task.handle.take() {
let _ = handle.join();
}
let st = task
.state
.lock()
.map_err(|_| "subtask fn: lock poisoned".to_string())?;
match &*st {
FnState::Done(v) => Ok(v.clone()),
FnState::Failed(e) => Err(e.clone()),
FnState::Killed => Err("subtask fn killed".into()),
FnState::Running => Err("subtask fn still running".into()),
}
}
pub fn kill(ctx: &mut HostContext, id: &Value) -> Result<Value, String> {
let id = child_id(id)?;
let handle = ctx
.subtasks
.remove(&id)
.ok_or_else(|| format!("subtask: unknown id {id}"))?;
match handle {
Handle::File(mut task) => {
task.child
.kill()
.map_err(|e| format!("subtask kill: {e}"))?;
let _ = task.child.wait();
stop_event_forward(&mut task);
if let Some(h) = task.stdout.take() {
let _ = h.join();
}
if let Some(h) = task.stderr.take() {
let _ = h.join();
}
let _ = std::fs::remove_file(&task.result_path);
}
Handle::Foreign(mut task) => {
task.child.kill().map_err(|e| format!("subtask kill: {e}"))?;
let _ = task.child.wait();
let _ = std::fs::remove_file(&task.temp_script);
let _ = task.stdout.join();
let _ = task.stderr.join();
}
Handle::Function(mut task) => {
if let Ok(mut st) = task.state.lock() {
if matches!(*st, FnState::Running) {
*st = FnState::Killed;
}
}
drop(task.handle.take());
}
}
Ok(Value::None)
}
pub fn wait_all(ctx: &mut HostContext) -> Result<Value, String> {
let ids: Vec<u64> = ctx.subtasks.keys().copied().collect();
let mut results = Vec::new();
for id in ids {
results.push(join(ctx, &Value::Int(id as i64))?);
}
Ok(Value::List(results))
}
fn capture_file_io(reader:&mut impl Read)->String {
let mut kept=Vec::new(); let mut buffer=[0u8;8192];
loop { match reader.read(&mut buffer) {
Ok(0)|Err(_)=>break,
Ok(n)=>{ let take=(FILE_IO_CAP+4-kept.len()).min(n);kept.extend_from_slice(&buffer[..take]); }
}}
clip_io(&String::from_utf8_lossy(&kept))
}
/// Timeout leaves the handle available unless cancel=True. Process cancellation
/// reaps the OS child; function-thread cancellation is logical, as in kill().
pub(super) fn wait_timeout(ctx:&mut HostContext,id:&Value,timeout_ms:u64,cancel:bool)->Result<Value,String>{
let start=std::time::Instant::now();
loop{
let status=poll(ctx,id)?;
let running=matches!(&status,Value::Map(fields) if fields.iter().any(|(k,v)|k=="status"&&v==&Value::Text("running".into())));
if !running{return join(ctx,id)}
if start.elapsed()>=Duration::from_millis(timeout_ms){if cancel{kill(ctx,id)?;}return Err("timed_out: subtask wait deadline exceeded".into())}
thread::sleep(Duration::from_millis(5));
}
}
pub(super) fn run_many(ctx:&mut HostContext,tasks:&Value,limit:usize,timeout_ms:u64)->Result<Value,String>{
let Value::List(tasks)=tasks else{return Err("tasks must be record list".into())};
if !(1..=64).contains(&limit){return Err("limit must be 1..64".into())}
if tasks.len()>10000{return Err("batch exceeds 10000 tasks".into())}
// Validate the entire batch before starting any process.
let jobs=tasks.iter().map(|task|{let Value::Map(fields)=task else{return Err("task must contain path and optional args".to_string())};
let mut job=HashMap::new();for (key,value) in fields{if !matches!(key.as_str(),"path"|"args"){return Err(format!("unsupported batch field {key}"))}job.insert(key.clone(),value.clone());}
let path=job.get("path").ok_or("task missing path")?;ctx.resolve_path(as_text(path,"path")?)?;Ok(job)
}).collect::<Result<Vec<_>,String>>()?;
let mut active:Vec<(usize,Value,std::time::Instant)>=Vec::new();let mut next=0;let mut out=vec![Value::None;jobs.len()];
while next<jobs.len()||!active.is_empty(){
while next<jobs.len()&&active.len()<limit{
match spawn(ctx,&jobs[next]){Ok(id)=>active.push((next,id,std::time::Instant::now())),Err(error)=>out[next]=super::result::capture("subtask.run_many",Err(error))};next+=1;
}
let mut i=0;
while i<active.len(){
let (_,id,start)=&active[i];let status=poll(ctx,id)?;
let running=matches!(&status,Value::Map(fields) if fields.iter().any(|(k,v)|k=="status"&&v==&Value::Text("running".into())));
if !running{let(index,id,_)=active.remove(i);out[index]=super::result::capture("subtask.run_many",join(ctx,&id));}
else if start.elapsed()>=Duration::from_millis(timeout_ms){let(index,id,_)=active.remove(i);let _=kill(ctx,&id);out[index]=super::result::capture("subtask.run_many",Err("timed_out: batch task deadline exceeded".into()));}
else{i+=1;}
}
if !active.is_empty(){thread::sleep(Duration::from_millis(5));}
}
Ok(Value::List(out))
}
fn clip_io(s: &str) -> String {
let mut it = s.chars();
let head: String = it.by_ref().take(FILE_IO_CAP).collect();
if it.next().is_some() {
format!("{head}\n…(truncated)")
} else {
head
}
}
fn require<'a>(bound: &'a HashMap<String, Value>, key: &str) -> Result<&'a Value, String> {
bound
.get(key)
.ok_or_else(|| format!("host call missing `{key}`"))
}
/// Propagate run/selection identity onto the *child* Command.env map.
/// Reads parent env only as a fallback when an explicit RunContext was not
/// applied; never uses parent `setenv`/`set_current_dir` as request state (M-R1).
fn apply_run_selection_env(cmd: &mut Command) {
for key in [
"MARQDO_RUN_ID",
"MARQDO_PARENT_RUN_ID",
"MARQDO_TURN_ID",
"MARQDO_WORKSPACE_SCOPE",
"MARQDO_CANCEL_TOKEN",
"MARQDO_EVENT_FORWARD",
"MARQDO_USAGE_SINK",
"MARQDO_CREDENTIAL_REF",
"MARQDO_WORKSPACE_ID",
"MARQDO_SNAPSHOT_ID",
"MARQDO_LEASE_ID",
"MARQDO_ATTEMPT_ID",
"MARQDO_LLM_MODEL",
"MARQDO_LLM_BASE_URL",
"OPENAI_MODEL",
"OPENAI_BASE_URL",
] {
if let Ok(v) = std::env::var(key) {
cmd.env(key, v);
}
}
// Do not forward process-global MARQDO_CANCEL into children — request cancel
// must use MARQDO_CANCEL_TOKEN so leases stay isolated.
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
use std::process::Command;
#[test]
fn f02_apply_run_selection_env_forwards_parent_identity() {
std::env::set_var("MARQDO_RUN_ID", "run-parent");
std::env::set_var("OPENAI_MODEL", "child-model");
std::env::set_var("OPENAI_BASE_URL", "http://127.0.0.1:7010");
// Must never require writing secrets into child env map for selection.
std::env::remove_var("OPENAI_API_KEY");
let mut cmd = Command::new("true");
apply_run_selection_env(&mut cmd);
// Command doesn't expose env for inspection portably; re-check via helper contract:
let keys: HashMap<&str, String> = [
"MARQDO_RUN_ID",
"OPENAI_MODEL",
"OPENAI_BASE_URL",
]
.into_iter()
.filter_map(|k| std::env::var(k).ok().map(|v| (k, v)))
.collect();
assert_eq!(keys.get("MARQDO_RUN_ID").map(String::as_str), Some("run-parent"));
assert_eq!(keys.get("OPENAI_MODEL").map(String::as_str), Some("child-model"));
assert_eq!(
keys.get("OPENAI_BASE_URL").map(String::as_str),
Some("http://127.0.0.1:7010")
);
std::env::remove_var("MARQDO_RUN_ID");
std::env::remove_var("OPENAI_MODEL");
std::env::remove_var("OPENAI_BASE_URL");
let _ = cmd; // ensure apply didn't panic
}
#[test]
fn f02_agent_skeleton_has_no_llm_llm() {
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let agent = std::fs::read_to_string(root.join("ext/ai/agent.mq.md")).unwrap();
assert!(
!agent.contains("llm.llm"),
"render_workbook_skeleton must not hardcode llm.llm"
);
assert!(
agent.contains("llm.create") && agent.contains("OPENAI_MODEL"),
"skeleton should create model from env selection"
);
}
}