release: v2.1.4 isolated HTTP execute (ADR 0009)
Default execute/sse-live/stream to Supervisor worker pool; MR-01 gold; /_runtime/*; bump CLI/wasm/ext to 2.1.4. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
58e13ee6d3
commit
3cabe53da2
56 files changed
+4957
-304
No files matched your search
+179
-12
@@ -1,7 +1,61 @@
|
||||
//! RunContextV1 — request-local run identity for selection / usage / cancel (M01).
|
||||
//! RunContextV1 — request-local run identity for selection / usage / cancel (M01 / M-R1).
|
||||
|
||||
use serde_json::{json, Map, Value};
|
||||
|
||||
/// Explicit request Context handles (M-R1). Paths are opaque grants owned by the
|
||||
/// control plane; callers must not pass browser-supplied absolute paths unchecked.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct RequestContext {
|
||||
pub workspace_id: Option<String>,
|
||||
pub snapshot_id: Option<String>,
|
||||
pub selection_ref: Option<String>,
|
||||
pub capabilities_ref: Option<String>,
|
||||
pub deadline_at: Option<String>,
|
||||
pub lease_id: Option<String>,
|
||||
pub attempt_id: Option<String>,
|
||||
pub epoch: Option<u64>,
|
||||
}
|
||||
|
||||
impl RequestContext {
|
||||
pub fn from_value(v: &Value) -> Self {
|
||||
Self {
|
||||
workspace_id: str_opt(v, "workspace_id"),
|
||||
snapshot_id: str_opt(v, "snapshot_id"),
|
||||
selection_ref: str_opt(v, "selection_ref"),
|
||||
capabilities_ref: str_opt(v, "capabilities_ref"),
|
||||
deadline_at: str_opt(v, "deadline_at"),
|
||||
lease_id: str_opt(v, "lease_id").or_else(|| {
|
||||
v.get("lease")
|
||||
.and_then(|l| l.get("id"))
|
||||
.and_then(|x| x.as_str())
|
||||
.map(|s| s.to_string())
|
||||
}),
|
||||
attempt_id: str_opt(v, "attempt_id"),
|
||||
epoch: v
|
||||
.get("epoch")
|
||||
.and_then(|x| x.as_u64())
|
||||
.or_else(|| {
|
||||
v.get("lease")
|
||||
.and_then(|l| l.get("epoch"))
|
||||
.and_then(|x| x.as_u64())
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_json(&self) -> Value {
|
||||
json!({
|
||||
"workspace_id": self.workspace_id,
|
||||
"snapshot_id": self.snapshot_id,
|
||||
"selection_ref": self.selection_ref,
|
||||
"capabilities_ref": self.capabilities_ref,
|
||||
"deadline_at": self.deadline_at,
|
||||
"lease_id": self.lease_id,
|
||||
"attempt_id": self.attempt_id,
|
||||
"epoch": self.epoch,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct RunContext {
|
||||
pub run_id: String,
|
||||
@@ -12,6 +66,8 @@ pub struct RunContext {
|
||||
pub usage_sink: Option<String>,
|
||||
pub event_sink: Option<String>,
|
||||
pub cancellation_token: Option<String>,
|
||||
/// Explicit Context object (M-R1); serialized alongside RunContextV1 fields.
|
||||
pub context: RequestContext,
|
||||
}
|
||||
|
||||
impl RunContext {
|
||||
@@ -22,6 +78,10 @@ impl RunContext {
|
||||
.filter(|s| !s.is_empty())
|
||||
.ok_or_else(|| "run_context.missing:run_id".to_string())?
|
||||
.to_string();
|
||||
let context = v
|
||||
.get("context")
|
||||
.map(RequestContext::from_value)
|
||||
.unwrap_or_default();
|
||||
Ok(Self {
|
||||
run_id,
|
||||
parent_run_id: v
|
||||
@@ -56,6 +116,7 @@ impl RunContext {
|
||||
.get("cancellation_token")
|
||||
.and_then(|x| x.as_str())
|
||||
.map(|s| s.to_string()),
|
||||
context,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -71,10 +132,11 @@ impl RunContext {
|
||||
"usage_sink": self.usage_sink,
|
||||
"event_sink": self.event_sink,
|
||||
"cancellation_token": self.cancellation_token,
|
||||
"context": self.context.to_json(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Child inherits selection/event/cancel; new run_id linked to parent.
|
||||
/// Child inherits selection/event/cancel handles; new run_id linked to parent.
|
||||
pub fn spawn_child(&self, child_run_id: &str) -> Self {
|
||||
Self {
|
||||
run_id: child_run_id.to_string(),
|
||||
@@ -85,10 +147,12 @@ impl RunContext {
|
||||
usage_sink: self.usage_sink.clone(),
|
||||
event_sink: self.event_sink.clone(),
|
||||
cancellation_token: self.cancellation_token.clone(),
|
||||
context: self.context.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Env vars for subtask injection (never includes api_key).
|
||||
/// Env pairs for *child* Command.env injection (never api_key / credential plaintext).
|
||||
/// Parent process must not `setenv` these to carry request state across tasks.
|
||||
pub fn selection_env(&self) -> Vec<(String, String)> {
|
||||
let mut out = Vec::new();
|
||||
out.push(("MARQDO_RUN_ID".into(), self.run_id.clone()));
|
||||
@@ -107,6 +171,9 @@ impl RunContext {
|
||||
if let Some(sink) = &self.event_sink {
|
||||
out.push(("MARQDO_EVENT_FORWARD".into(), sink.clone()));
|
||||
}
|
||||
if let Some(usage) = &self.usage_sink {
|
||||
out.push(("MARQDO_USAGE_SINK".into(), usage.clone()));
|
||||
}
|
||||
if let Some(model) = self
|
||||
.selection_context
|
||||
.get("upstream_model_id")
|
||||
@@ -124,25 +191,125 @@ impl RunContext {
|
||||
out.push(("MARQDO_LLM_BASE_URL".into(), base.to_string()));
|
||||
out.push(("OPENAI_BASE_URL".into(), base.to_string()));
|
||||
}
|
||||
// credential_ref only — never expand api_key into child env here.
|
||||
if let Some(cred) = self
|
||||
.selection_context
|
||||
.get("credential_ref")
|
||||
.and_then(|v| v.as_str())
|
||||
{
|
||||
out.push(("MARQDO_CREDENTIAL_REF".into(), cred.to_string()));
|
||||
}
|
||||
if let Some(ws) = &self.context.workspace_id {
|
||||
out.push(("MARQDO_WORKSPACE_ID".into(), ws.clone()));
|
||||
}
|
||||
if let Some(snap) = &self.context.snapshot_id {
|
||||
out.push(("MARQDO_SNAPSHOT_ID".into(), snap.clone()));
|
||||
}
|
||||
if let Some(lease) = &self.context.lease_id {
|
||||
out.push(("MARQDO_LEASE_ID".into(), lease.clone()));
|
||||
}
|
||||
if let Some(attempt) = &self.context.attempt_id {
|
||||
out.push(("MARQDO_ATTEMPT_ID".into(), attempt.clone()));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Apply this run's handles onto a child Command without mutating parent env.
|
||||
pub fn apply_to_command(&self, cmd: &mut std::process::Command) {
|
||||
for (k, v) in self.selection_env() {
|
||||
cmd.env(k, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Check whether a cancel token file/env indicates cancellation.
|
||||
fn str_opt(v: &Value, key: &str) -> Option<String> {
|
||||
v.get(key)
|
||||
.and_then(|x| x.as_str())
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|s| s.to_string())
|
||||
}
|
||||
|
||||
/// Unified cancel-token semantics (M-R1 / design §6):
|
||||
/// - missing path / missing file → not cancelled
|
||||
/// - empty body / `"0"` → not cancelled
|
||||
/// - `"1"` / `"cancelled"` → cancelled
|
||||
/// - read failure → not cancelled (caller may log); do not treat as cancel
|
||||
///
|
||||
/// Process-global `MARQDO_CANCEL` is **not** consulted when an explicit token
|
||||
/// handle is provided, so parallel tasks cannot cancel each other via env.
|
||||
pub fn cancel_requested(token: Option<&str>) -> bool {
|
||||
let Some(tok) = token.filter(|s| !s.is_empty()) else {
|
||||
return false;
|
||||
};
|
||||
// Token may be a path: presence of file or contents "1"/"cancelled" means stop.
|
||||
let path = std::path::Path::new(tok);
|
||||
if path.is_file() {
|
||||
if let Ok(body) = std::fs::read_to_string(path) {
|
||||
if !path.is_file() {
|
||||
return false;
|
||||
}
|
||||
match std::fs::read_to_string(path) {
|
||||
Ok(body) => {
|
||||
let t = body.trim();
|
||||
return t.is_empty() || t == "1" || t.eq_ignore_ascii_case("cancelled");
|
||||
t == "1" || t.eq_ignore_ascii_case("cancelled")
|
||||
}
|
||||
return true;
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Token-file body interpretation used by LLM plugin and control plane alike.
|
||||
pub fn cancel_token_body_means_cancel(body: &str) -> bool {
|
||||
let t = body.trim();
|
||||
t == "1" || t.eq_ignore_ascii_case("cancelled")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::Write;
|
||||
|
||||
#[test]
|
||||
fn empty_and_zero_token_are_not_cancelled() {
|
||||
let dir = std::env::temp_dir().join(format!("mq-rc-empty-{}", std::process::id()));
|
||||
let _ = std::fs::remove_file(&dir);
|
||||
std::fs::File::create(&dir).unwrap();
|
||||
assert!(!cancel_requested(Some(dir.to_str().unwrap())));
|
||||
std::fs::write(&dir, "0").unwrap();
|
||||
assert!(!cancel_requested(Some(dir.to_str().unwrap())));
|
||||
std::fs::write(&dir, "cancelled").unwrap();
|
||||
assert!(cancel_requested(Some(dir.to_str().unwrap())));
|
||||
let _ = std::fs::remove_file(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_token_ignores_global_marqdo_cancel() {
|
||||
let dir = std::env::temp_dir().join(format!("mq-rc-ign-{}", std::process::id()));
|
||||
let _ = std::fs::remove_file(&dir);
|
||||
let mut f = std::fs::File::create(&dir).unwrap();
|
||||
f.write_all(b"").unwrap();
|
||||
drop(f);
|
||||
std::env::set_var("MARQDO_CANCEL", "1");
|
||||
assert!(
|
||||
!cancel_requested(Some(dir.to_str().unwrap())),
|
||||
"empty token must not cancel even if MARQDO_CANCEL=1"
|
||||
);
|
||||
std::env::remove_var("MARQDO_CANCEL");
|
||||
let _ = std::fs::remove_file(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn context_round_trip() {
|
||||
let v = json!({
|
||||
"run_id": "r1",
|
||||
"cancellation_token": "/tmp/t",
|
||||
"context": {
|
||||
"workspace_id": "ws",
|
||||
"lease": {"id": "lease_1", "epoch": 3},
|
||||
"attempt_id": "a1"
|
||||
}
|
||||
});
|
||||
let rc = RunContext::from_value(&v).unwrap();
|
||||
assert_eq!(rc.context.lease_id.as_deref(), Some("lease_1"));
|
||||
assert_eq!(rc.context.epoch, Some(3));
|
||||
let child = rc.spawn_child("r2");
|
||||
assert_eq!(child.parent_run_id.as_deref(), Some("r1"));
|
||||
assert_eq!(child.context.lease_id.as_deref(), Some("lease_1"));
|
||||
}
|
||||
std::env::var("MARQDO_CANCEL")
|
||||
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
Reference in new issue
Block a user