Skip to main content

graphrt_code/
lib.rs

1//! # graphrt-code
2//!
3//! The `code` node: runs user-supplied code over workflow variables.
4//!
5//! - **JavaScript** runs in an embedded QuickJS engine (feature `js`, on by
6//!   default). The engine has no file system, network, timers or module
7//!   loader; memory, stack and execution time are capped.
8//! - **Other languages** (e.g. `python3`) run in a host-provided
9//!   [`RemoteSandbox`] service. The optional `remote` feature provides
10//!   `HttpSandbox`, a client for an HTTP sandbox service. The optional `python`
11//!   feature provides `SubprocessPython`, which runs a local `python3` with
12//!   resource limits — **not a security sandbox**, for trusted code only.
13//!
14//! User code defines `main(args)`, which receives an object with the configured
15//! input variables and returns an object (or a promise of one):
16//!
17//! ```js
18//! function main({ a, b }) {
19//!   return { sum: a + b };
20//! }
21//! ```
22//!
23//! Inputs are passed with secrets **revealed**: code nodes frequently need API
24//! keys from `env.*`. Returned strings that contain one of those secrets (alone or
25//! inside a larger string) become secrets again, so they are masked in events and
26//! records; captured stdout is scrubbed the same way. A script can still leak a
27//! transformed secret (reversed, encoded), so do not give untrusted code keys.
28
29use std::fmt;
30use std::sync::Arc;
31
32use futures::future::BoxFuture;
33use indexmap::IndexMap;
34use serde::Deserialize;
35
36use graphrt_core::dsl::NodeDoc;
37use graphrt_core::node::{ConfigError, Node, NodeContext, NodeError, NodeOutput, NodeResult, Registry, parse_config};
38use graphrt_core::{Selector, Value, ValueType};
39
40#[cfg(feature = "js")]
41mod js;
42#[cfg(feature = "python")]
43mod python;
44#[cfg(feature = "remote")]
45mod remote;
46
47#[cfg(feature = "python")]
48pub use python::SubprocessPython;
49#[cfg(feature = "remote")]
50pub use remote::HttpSandbox;
51
52/// Node kind handled by this crate.
53pub const KIND: &str = "code";
54
55/// Resource limits for one code execution.
56#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
57#[serde(default)]
58pub struct CodeLimits {
59    /// Wall-clock execution budget in milliseconds.
60    pub time_ms: u64,
61    /// Heap limit in bytes.
62    pub memory_bytes: usize,
63    /// Native stack limit in bytes.
64    pub stack_bytes: usize,
65    /// Maximum size of the JSON-encoded result.
66    pub max_output_bytes: usize,
67    /// Maximum nesting depth of the result.
68    pub max_depth: usize,
69}
70
71impl CodeLimits {
72    /// Field-wise minimum of `self` and `max`: workflow-provided limits may only lower
73    /// the operator's limits, never raise them.
74    pub fn clamp_to(self, max: &CodeLimits) -> CodeLimits {
75        CodeLimits {
76            time_ms: self.time_ms.min(max.time_ms),
77            memory_bytes: self.memory_bytes.min(max.memory_bytes),
78            stack_bytes: self.stack_bytes.min(max.stack_bytes),
79            max_output_bytes: self.max_output_bytes.min(max.max_output_bytes),
80            max_depth: self.max_depth.min(max.max_depth),
81        }
82    }
83}
84
85impl Default for CodeLimits {
86    fn default() -> Self {
87        Self {
88            time_ms: 10_000,
89            memory_bytes: 64 * 1024 * 1024,
90            stack_bytes: 1024 * 1024,
91            max_output_bytes: 4 * 1024 * 1024,
92            max_depth: 32,
93        }
94    }
95}
96
97/// A sandbox that executes code out of process (for languages without an embedded engine).
98///
99/// Register it as `Arc<dyn RemoteSandbox>` in [`graphrt_core::Services`].
100pub trait RemoteSandbox: Send + Sync + fmt::Debug {
101    /// Execute `code` in `language` with `inputs` (a JSON object). Must return the JSON
102    /// object produced by the code's `main` function.
103    fn execute<'a>(
104        &'a self,
105        language: &'a str,
106        code: &'a str,
107        inputs: serde_json::Value,
108        limits: CodeLimits,
109    ) -> BoxFuture<'a, Result<serde_json::Value, NodeError>>;
110
111    /// Like [`RemoteSandbox::execute`], also returning diagnostics (captured output,
112    /// limits that could not be applied). The default reports none.
113    fn execute_detailed<'a>(
114        &'a self,
115        language: &'a str,
116        code: &'a str,
117        inputs: serde_json::Value,
118        limits: CodeLimits,
119    ) -> BoxFuture<'a, Result<SandboxRun, NodeError>> {
120        Box::pin(async move {
121            let result = self.execute(language, code, inputs, limits).await?;
122            Ok(SandboxRun { result, stdout: None, notes: Vec::new() })
123        })
124    }
125}
126
127/// Result of [`RemoteSandbox::execute_detailed`].
128#[derive(Clone, Debug, Default, PartialEq)]
129pub struct SandboxRun {
130    /// The object returned by `main`.
131    pub result: serde_json::Value,
132    /// What the code printed (capped), if captured.
133    pub stdout: Option<String>,
134    /// Operator-facing notes, e.g. resource limits the platform could not apply.
135    pub notes: Vec<String>,
136}
137
138/// Where code runs.
139#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize)]
140#[serde(rename_all = "snake_case")]
141pub enum SandboxChoice {
142    /// Embedded engine when available for the language, otherwise the remote sandbox.
143    #[default]
144    Auto,
145    /// Always use the registered [`RemoteSandbox`].
146    Remote,
147}
148
149#[derive(Clone, Debug, Deserialize)]
150struct InputVar {
151    variable: String,
152    value_selector: Selector,
153}
154
155#[derive(Clone, Debug, Deserialize)]
156struct OutputDecl {
157    #[serde(rename = "type")]
158    ty: ValueType,
159}
160
161#[derive(Clone, Debug, Deserialize)]
162struct CodeConfig {
163    #[serde(default = "default_language")]
164    code_language: String,
165    code: String,
166    #[serde(default)]
167    variables: Vec<InputVar>,
168    #[serde(default)]
169    outputs: IndexMap<String, OutputDecl>,
170    #[serde(default)]
171    limits: Option<CodeLimits>,
172    #[serde(default)]
173    sandbox: SandboxChoice,
174}
175
176fn default_language() -> String {
177    "javascript".to_owned()
178}
179
180/// Normalized language name.
181fn normalize_language(lang: &str) -> String {
182    match lang.trim().to_ascii_lowercase().as_str() {
183        "js" | "javascript" | "nodejs" | "node" => "javascript".to_owned(),
184        "py" | "python" | "python3" => "python3".to_owned(),
185        other => other.to_owned(),
186    }
187}
188
189/// The `code` node.
190#[derive(Debug)]
191pub struct CodeNode {
192    language: String,
193    code: Arc<str>,
194    variables: Vec<InputVar>,
195    outputs: IndexMap<String, OutputDecl>,
196    limits: Option<CodeLimits>,
197    sandbox: SandboxChoice,
198}
199
200impl CodeNode {
201    /// Build from a node document.
202    pub fn from_doc(doc: &NodeDoc) -> Result<Self, ConfigError> {
203        let cfg: CodeConfig = parse_config(doc)?;
204        if cfg.code.trim().is_empty() {
205            return Err(ConfigError::new(doc, "`code` must not be empty"));
206        }
207        let language = normalize_language(&cfg.code_language);
208        if language == "jinja2" {
209            return Err(ConfigError::new(doc, "jinja2 is not a code language; use a template-transform node"));
210        }
211        Ok(Self {
212            language,
213            code: Arc::from(cfg.code),
214            variables: cfg.variables,
215            outputs: cfg.outputs,
216            limits: cfg.limits,
217            sandbox: cfg.sandbox,
218        })
219    }
220
221    /// Effective limits: the host's [`CodeLimits`] service (or the defaults) is the
222    /// ceiling; limits in the node config can only lower it. The time limit is further
223    /// capped by the attempt deadline so a node timeout also interrupts the interpreter
224    /// (which blocks synchronously).
225    fn effective_limits(&self, ctx: &NodeContext) -> CodeLimits {
226        let host = ctx.service::<CodeLimits>().copied().unwrap_or_default();
227        let mut limits = match self.limits {
228            Some(requested) => requested.clamp_to(&host),
229            None => host,
230        };
231        if let Some(remaining) = ctx.remaining() {
232            let remaining_ms = u64::try_from(remaining.as_millis()).unwrap_or(u64::MAX).max(1);
233            limits.time_ms = limits.time_ms.min(remaining_ms);
234        }
235        limits
236    }
237
238    /// Build the `main(args)` object. Selectors that do not resolve are passed as
239    /// `null` and returned in the second element (reported as `missing_inputs` metadata).
240    fn build_inputs(&self, ctx: &NodeContext) -> (serde_json::Value, Vec<String>) {
241        let mut args = serde_json::Map::new();
242        let mut missing = Vec::new();
243        for var in &self.variables {
244            let value = match ctx.get(&var.value_selector) {
245                Some(v) => v,
246                None => {
247                    missing.push(var.value_selector.to_string());
248                    Value::Null
249                }
250            };
251            args.insert(var.variable.clone(), value.to_json_exposed());
252        }
253        (serde_json::Value::Object(args), missing)
254    }
255
256    /// Plaintext of the secrets passed to the code (revealed in its inputs).
257    fn input_secrets(&self, ctx: &NodeContext) -> Vec<Arc<str>> {
258        let mut secrets = Vec::new();
259        for var in &self.variables {
260            if let Some(value) = ctx.get(&var.value_selector) {
261                collect_secrets(&value, &mut secrets);
262            }
263        }
264        secrets.retain(|s| s.len() >= graphrt_core::redact::MIN_SCRUB_LEN);
265        secrets
266    }
267
268    async fn execute(&self, ctx: &NodeContext, inputs: serde_json::Value) -> Result<SandboxRun, NodeError> {
269        let limits = self.effective_limits(ctx);
270        let use_remote = self.sandbox == SandboxChoice::Remote || !embedded_supports(&self.language);
271        if use_remote {
272            let sandbox = ctx.service::<Arc<dyn RemoteSandbox>>().ok_or_else(|| {
273                NodeError::fatal(
274                    "missing_service",
275                    format!(
276                        "{} code requires a remote sandbox; register an `Arc<dyn RemoteSandbox>` service",
277                        self.language
278                    ),
279                )
280            })?;
281            return sandbox.execute_detailed(&self.language, &self.code, inputs, limits).await;
282        }
283        let result = run_embedded(self.code.clone(), inputs, limits).await?;
284        Ok(SandboxRun { result, stdout: None, notes: Vec::new() })
285    }
286
287    fn convert_outputs(&self, result: serde_json::Value, limits: &CodeLimits) -> Result<NodeOutput, NodeError> {
288        let serde_json::Value::Object(map) = result else {
289            return Err(NodeError::fatal("invalid_output", "main() must return an object"));
290        };
291        let depth = json_depth(&serde_json::Value::Object(map.clone()));
292        if depth > limits.max_depth {
293            return Err(NodeError::fatal(
294                "invalid_output",
295                format!("result nesting depth {depth} exceeds the limit of {}", limits.max_depth),
296            ));
297        }
298        let mut output = NodeOutput::new();
299        if self.outputs.is_empty() {
300            for (key, value) in map {
301                output.outputs.insert(key, Value::from_json(value));
302            }
303            return Ok(output);
304        }
305        for (name, decl) in &self.outputs {
306            let raw = map
307                .get(name)
308                .cloned()
309                .ok_or_else(|| NodeError::fatal("invalid_output", format!("missing output `{name}`")))?;
310            let value = decl
311                .ty
312                .coerce(Value::from_json(raw))
313                .map_err(|e| NodeError::fatal("invalid_output", format!("output `{name}`: {e}")))?;
314            output.outputs.insert(name.clone(), value);
315        }
316        let ignored: Vec<&String> = map.keys().filter(|k| !self.outputs.contains_key(*k)).collect();
317        if !ignored.is_empty() {
318            let names = ignored.iter().map(|k| Value::from(k.as_str()));
319            output.metadata.insert("ignored_outputs".to_owned(), Value::array(names));
320        }
321        Ok(output)
322    }
323}
324
325impl Node for CodeNode {
326    fn run(&self, ctx: NodeContext) -> BoxFuture<'_, NodeResult> {
327        Box::pin(async move {
328            let limits = self.effective_limits(&ctx);
329            let (inputs, missing) = self.build_inputs(&ctx);
330            let secrets = self.input_secrets(&ctx);
331            let run = self.execute(&ctx, inputs).await?;
332            let mut output = self.convert_outputs(run.result, &limits)?;
333            output.outputs = output.outputs.into_iter().map(|(k, v)| (k, retag_secrets(v, &secrets))).collect();
334            if let Some(stdout) = run.stdout.filter(|s| !s.is_empty()) {
335                let stdout = graphrt_core::redact::redact_secrets(&stdout, &secrets).into_owned();
336                output = output.meta("stdout", stdout);
337            }
338            if !run.notes.is_empty() {
339                output = output.meta("sandbox_notes", Value::array(run.notes.into_iter().map(Value::from)));
340            }
341            if !missing.is_empty() {
342                output = output.meta("missing_inputs", Value::array(missing.into_iter().map(Value::from)));
343            }
344            Ok(output.meta("language", self.language.as_str()).into())
345        })
346    }
347
348    fn inputs(&self) -> Vec<Selector> {
349        self.variables.iter().map(|v| v.value_selector.clone()).collect()
350    }
351}
352
353fn embedded_supports(language: &str) -> bool {
354    cfg!(feature = "js") && language == "javascript"
355}
356
357#[cfg(feature = "js")]
358async fn run_embedded(
359    code: Arc<str>,
360    inputs: serde_json::Value,
361    limits: CodeLimits,
362) -> Result<serde_json::Value, NodeError> {
363    let input_text = serde_json::to_string(&inputs)
364        .map_err(|e| NodeError::fatal("invalid_input", format!("cannot encode inputs: {e}")))?;
365
366    // QuickJS is synchronous and checks its stack limit against the thread it runs on.
367    // A dedicated thread sized from the limit keeps the executor free and guarantees
368    // the engine raises a catchable `RangeError` before the native stack overflows
369    // (which would abort the whole process), whatever the caller's thread stack is.
370    let (sender, receiver) = futures::channel::oneshot::channel();
371    std::thread::Builder::new()
372        .name("graphrt-js".to_owned())
373        .stack_size(js::thread_stack_bytes(&limits))
374        .spawn(move || {
375            // The receiver is gone only if the node was cancelled; nothing to report then.
376            let _ = sender.send(js::run(&code, &input_text, &limits));
377        })
378        .map_err(|e| NodeError::new("sandbox", format!("cannot start the code execution thread: {e}")))?;
379    receiver.await.map_err(|_| NodeError::new("sandbox", "code execution thread stopped without a result"))?
380}
381
382#[cfg(not(feature = "js"))]
383async fn run_embedded(
384    _code: Arc<str>,
385    _inputs: serde_json::Value,
386    _limits: CodeLimits,
387) -> Result<serde_json::Value, NodeError> {
388    Err(NodeError::fatal("unsupported", "embedded JavaScript is disabled (feature `js`)"))
389}
390
391fn collect_secrets(value: &Value, out: &mut Vec<Arc<str>>) {
392    match value {
393        Value::Secret(plaintext) => out.push(Arc::clone(plaintext)),
394        Value::Array(items) => items.iter().for_each(|item| collect_secrets(item, out)),
395        Value::Object(map) => map.values().for_each(|item| collect_secrets(item, out)),
396        _ => {}
397    }
398}
399
400/// Code receives secrets revealed and can return them, alone or inside a larger
401/// string (`"Bearer " + key`). Such strings become secrets again, so they stay
402/// usable by downstream nodes but are masked in events, run records and snapshots.
403fn retag_secrets(value: Value, secrets: &[Arc<str>]) -> Value {
404    if secrets.is_empty() {
405        return value;
406    }
407    match value {
408        Value::String(text) if secrets.iter().any(|s| text.contains(s.as_ref())) => {
409            Value::Secret(Arc::from(text.as_ref()))
410        }
411        Value::Array(items) => Value::array(items.iter().cloned().map(|item| retag_secrets(item, secrets))),
412        Value::Object(map) => {
413            let map: graphrt_core::value::Map =
414                map.iter().map(|(k, v)| (k.clone(), retag_secrets(v.clone(), secrets))).collect();
415            Value::Object(Arc::new(map))
416        }
417        other => other,
418    }
419}
420
421fn json_depth(value: &serde_json::Value) -> usize {
422    match value {
423        serde_json::Value::Array(items) => 1 + items.iter().map(json_depth).max().unwrap_or(0),
424        serde_json::Value::Object(map) => 1 + map.values().map(json_depth).max().unwrap_or(0),
425        _ => 0,
426    }
427}
428
429/// Register the `code` node kind.
430pub fn register(registry: &mut Registry) {
431    registry.register_lazily_described(KIND, descriptor, |doc| Ok(Arc::new(CodeNode::from_doc(doc)?) as Arc<dyn Node>));
432}
433
434/// Catalog descriptor of the `code` node.
435pub fn descriptor() -> graphrt_core::catalog::NodeDescriptor {
436    use graphrt_core::catalog::schema::{nested, object, value_type, variable_bindings};
437    use graphrt_core::catalog::{NodeCategory, NodeDescriptor};
438    use serde_json::json;
439
440    let limit = |what: &str| json!({ "type": "integer", "minimum": 0, "description": what });
441    NodeDescriptor::new(KIND, "Code", NodeCategory::Integration)
442        .describe(
443            "Runs `main(args)` over bound variables. JavaScript runs in an embedded QuickJS engine with \
444             no I/O and memory/time limits; other languages (python3) need a sandbox service. Declared \
445             outputs are type-checked; without declarations every returned key becomes an output.",
446        )
447        .icon("code")
448        .source("graphrt-code")
449        .schema(object(
450            json!({
451                "code_language": {
452                    "type": "string",
453                    "enum": ["javascript", "js", "nodejs", "node", "python3", "python", "py"],
454                    "default": "javascript"
455                },
456                "code": { "type": "string", "minLength": 1 },
457                "variables": variable_bindings(),
458                "outputs": { "type": "object", "additionalProperties": nested(json!({ "type": value_type() }), &["type"]) },
459                "limits": nested(json!({
460                    "time_ms": limit("Wall-clock budget (ms)"),
461                    "memory_bytes": limit("Heap limit"),
462                    "stack_bytes": limit("Stack limit"),
463                    "max_output_bytes": limit("Result size limit"),
464                    "max_depth": limit("Result nesting limit")
465                }), &[]),
466                "sandbox": { "type": "string", "enum": ["auto", "remote"], "default": "auto" }
467            }),
468            &["code"],
469        ))
470        .dynamic_outputs()
471}
472
473#[cfg(test)]
474mod tests {
475    use super::*;
476    use graphrt_core::node::NodeOutcome;
477    use graphrt_core::{Services, VariablePool};
478
479    fn node(config: serde_json::Value) -> Result<CodeNode, ConfigError> {
480        CodeNode::from_doc(&NodeDoc::new("c", KIND, config))
481    }
482
483    fn pool() -> VariablePool {
484        let mut p = VariablePool::new();
485        p.insert("start", "a", Value::Int(2));
486        p.insert("start", "b", Value::Int(3));
487        p.insert("start", "list", Value::from_json(serde_json::json!([1, 2, 3])));
488        p.insert("env", "KEY", Value::Secret(Arc::from("sk-live")));
489        p
490    }
491
492    async fn run(config: serde_json::Value) -> Result<NodeOutput, NodeError> {
493        let n = node(config).map_err(|e| NodeError::fatal("config", e.to_string()))?;
494        match n.run(NodeContext::for_test("c", pool())).await? {
495            NodeOutcome::Completed(o) => Ok(o),
496            NodeOutcome::Paused(_) => Err(NodeError::fatal("test", "unexpected pause")),
497        }
498    }
499
500    fn vars() -> serde_json::Value {
501        serde_json::json!([
502            {"variable": "a", "value_selector": ["start", "a"]},
503            {"variable": "b", "value_selector": ["start", "b"]},
504            {"variable": "list", "value_selector": ["start", "list"]},
505            {"variable": "key", "value_selector": ["env", "KEY"]},
506        ])
507    }
508
509    #[tokio::test]
510    async fn arithmetic() {
511        let out = run(serde_json::json!({
512            "code": "function main({a, b}) { return { sum: a + b, ratio: b / a }; }",
513            "variables": vars(),
514            "outputs": {"sum": {"type": "number"}, "ratio": {"type": "float"}},
515        }))
516        .await
517        .unwrap();
518        assert_eq!(out.outputs.get("sum"), Some(&Value::Int(5)));
519        assert_eq!(out.outputs.get("ratio"), Some(&Value::Float(1.5)));
520    }
521
522    #[tokio::test]
523    async fn object_and_array_returns_untyped() {
524        let out = run(serde_json::json!({
525            "code": "function main(args) { return { doubled: args.list.map(x => x * 2), obj: { k: 'v' } }; }",
526            "variables": vars(),
527        }))
528        .await
529        .unwrap();
530        assert_eq!(out.outputs.get("doubled"), Some(&Value::from_json(serde_json::json!([2, 4, 6]))));
531        assert_eq!(out.outputs.get("obj"), Some(&Value::from_json(serde_json::json!({"k": "v"}))));
532    }
533
534    #[tokio::test]
535    async fn async_main_is_supported() {
536        let out = run(serde_json::json!({
537            "code": "async function main({a}) { const v = await Promise.resolve(a * 10); return { v }; }",
538            "variables": vars(),
539        }))
540        .await
541        .unwrap();
542        assert_eq!(out.outputs.get("v"), Some(&Value::Int(20)));
543    }
544
545    #[tokio::test]
546    async fn secrets_are_available_to_code() {
547        let out = run(serde_json::json!({
548            "code": "function main({key}) { return { len: key.length }; }",
549            "variables": vars(),
550        }))
551        .await
552        .unwrap();
553        assert_eq!(out.outputs.get("len"), Some(&Value::Int(7)));
554    }
555
556    #[tokio::test]
557    async fn returned_secrets_are_masked_again() {
558        let out = run(serde_json::json!({
559            "code": "function main({key}) { return { raw: key, header: 'Bearer ' + key, nested: { list: [key, 'plain'] }, n: 1 }; }",
560            "variables": vars(),
561        }))
562        .await
563        .unwrap();
564        assert_eq!(out.outputs.get("raw"), Some(&Value::Secret(Arc::from("sk-live"))));
565        assert_eq!(out.outputs.get("header"), Some(&Value::Secret(Arc::from("Bearer sk-live"))));
566        assert_eq!(out.outputs.get("n"), Some(&Value::Int(1)));
567        let shown = serde_json::to_string(&out.outputs).unwrap();
568        assert!(!shown.contains("sk-live"), "{shown}");
569        assert!(shown.contains("\"plain\""), "{shown}");
570    }
571
572    #[tokio::test]
573    async fn type_validation_failure() {
574        let err = run(serde_json::json!({
575            "code": "function main() { return { n: 'not a number' }; }",
576            "outputs": {"n": {"type": "number"}},
577        }))
578        .await
579        .unwrap_err();
580        assert_eq!(err.kind, "invalid_output");
581        assert!(!err.retryable);
582    }
583
584    #[tokio::test]
585    async fn missing_declared_output() {
586        let err = run(serde_json::json!({
587            "code": "function main() { return { other: 1 }; }",
588            "outputs": {"n": {"type": "number"}},
589        }))
590        .await
591        .unwrap_err();
592        assert!(err.message.contains("missing output `n`"), "{err}");
593    }
594
595    #[tokio::test]
596    async fn non_object_result() {
597        let err = run(serde_json::json!({ "code": "function main() { return 42; }" })).await.unwrap_err();
598        assert!(err.message.contains("must return an object"), "{err}");
599    }
600
601    #[tokio::test]
602    async fn thrown_error_message_is_reported() {
603        let err =
604            run(serde_json::json!({ "code": "function main() { throw new Error('boom here'); }" })).await.unwrap_err();
605        assert_eq!(err.kind, "code_error");
606        assert!(err.message.contains("boom here"), "{err}");
607    }
608
609    #[tokio::test]
610    async fn syntax_error_is_reported() {
611        let err = run(serde_json::json!({ "code": "function main( { return {}; }" })).await.unwrap_err();
612        assert_eq!(err.kind, "code_error");
613    }
614
615    #[tokio::test]
616    async fn missing_main() {
617        let err = run(serde_json::json!({ "code": "function notMain() { return {}; }" })).await.unwrap_err();
618        assert!(err.message.contains("main"), "{err}");
619    }
620
621    #[tokio::test]
622    async fn infinite_loop_is_interrupted() {
623        let started = std::time::Instant::now();
624        let err = run(serde_json::json!({
625            "code": "function main() { while (true) {} }",
626            "limits": {"time_ms": 200},
627        }))
628        .await
629        .unwrap_err();
630        assert_eq!(err.kind, "timeout");
631        assert!(started.elapsed() < std::time::Duration::from_secs(5));
632    }
633
634    #[tokio::test]
635    async fn attempt_deadline_caps_the_time_limit() {
636        // The configured limit is generous, but the engine-provided deadline is not.
637        let n = node(serde_json::json!({"code": "function main() { while (true) {} }", "limits": {"time_ms": 60000}}))
638            .unwrap();
639        let ctx = NodeContext::for_test("c", pool());
640        let deadline = ctx.clock.now_ms() + 200;
641        let started = std::time::Instant::now();
642        let err = n.run(ctx.with_deadline(Some(deadline))).await.unwrap_err();
643        assert_eq!(err.kind, "timeout");
644        assert!(started.elapsed() < std::time::Duration::from_secs(5), "{:?}", started.elapsed());
645    }
646
647    #[tokio::test]
648    async fn top_level_infinite_loop_is_interrupted() {
649        let err = run(serde_json::json!({
650            "code": "while (true) {} function main() { return {}; }",
651            "limits": {"time_ms": 200},
652        }))
653        .await
654        .unwrap_err();
655        assert_eq!(err.kind, "timeout");
656    }
657
658    #[tokio::test]
659    async fn memory_bomb_is_limited() {
660        let err = run(serde_json::json!({
661            "code": "function main() { const a = []; while (true) { a.push('x'.repeat(1 << 20)); } }",
662            "limits": {"memory_bytes": 16777216, "time_ms": 5000},
663        }))
664        .await
665        .unwrap_err();
666        assert!(err.kind == "out_of_memory" || err.kind == "code_error", "{err}");
667        assert!(!err.retryable);
668    }
669
670    #[tokio::test]
671    async fn timeouts_are_not_retryable() {
672        let err = run(serde_json::json!({
673            "code": "function main() { while (true) {} }",
674            "limits": {"time_ms": 100},
675        }))
676        .await
677        .unwrap_err();
678        assert_eq!(err.kind, "timeout");
679        assert!(!err.retryable);
680    }
681
682    #[tokio::test]
683    async fn config_limits_cannot_exceed_host_limits() {
684        let n = node(serde_json::json!({
685            "code": "function main() { while (true) {} }",
686            "limits": {"time_ms": 3_600_000u64, "memory_bytes": 1_000_000_000_000u64},
687        }))
688        .unwrap();
689        let host = CodeLimits { time_ms: 150, ..CodeLimits::default() };
690        let services = Arc::new(Services::new().with(host));
691        let ctx = NodeContext::for_test("c", pool()).with_services(services.clone());
692        assert_eq!(n.effective_limits(&ctx).time_ms, 150);
693        assert_eq!(n.effective_limits(&ctx).memory_bytes, CodeLimits::default().memory_bytes);
694        let started = std::time::Instant::now();
695        let err = n.run(ctx).await.unwrap_err();
696        assert_eq!(err.kind, "timeout");
697        assert!(started.elapsed() < std::time::Duration::from_secs(5));
698
699        // Without a host service, the defaults are the ceiling.
700        let ctx = NodeContext::for_test("c", pool());
701        assert_eq!(n.effective_limits(&ctx).time_ms, CodeLimits::default().time_ms);
702    }
703
704    #[tokio::test]
705    async fn non_finite_numbers_are_rejected() {
706        let err = run(serde_json::json!({ "code": "function main() { return { x: 1 / 0 }; }" })).await.unwrap_err();
707        assert_eq!(err.kind, "code_error");
708        assert!(err.message.contains("non-finite"), "{err}");
709    }
710
711    #[tokio::test]
712    async fn user_errors_mentioning_memory_are_code_errors() {
713        let err =
714            run(serde_json::json!({ "code": "function main() { throw new Error('out of memory reading file'); }" }))
715                .await
716                .unwrap_err();
717        assert_eq!(err.kind, "code_error", "{err}");
718    }
719
720    #[tokio::test]
721    async fn thrown_non_error_values_are_described() {
722        let err = run(serde_json::json!({ "code": "function main() { throw 42; }" })).await.unwrap_err();
723        assert_eq!(err.kind, "code_error");
724        assert!(err.message.contains("42"), "{err}");
725    }
726
727    #[tokio::test]
728    async fn missing_inputs_are_reported() {
729        let out = run(serde_json::json!({
730            "code": "function main({ x }) { return { isNull: x === null }; }",
731            "variables": [{"variable": "x", "value_selector": ["start", "nope"]}],
732        }))
733        .await
734        .unwrap();
735        assert_eq!(out.outputs.get("isNull"), Some(&Value::Bool(true)));
736        assert_eq!(out.metadata.get("missing_inputs"), Some(&Value::array([Value::from("start.nope")])));
737    }
738
739    #[tokio::test]
740    async fn output_size_is_limited() {
741        let err = run(serde_json::json!({
742            "code": "function main() { return { s: 'x'.repeat(100000) }; }",
743            "limits": {"max_output_bytes": 1000},
744        }))
745        .await
746        .unwrap_err();
747        assert_eq!(err.kind, "invalid_output");
748    }
749
750    #[tokio::test]
751    async fn no_host_capabilities() {
752        let out = run(serde_json::json!({
753            "code": "function main() { return { t: typeof setTimeout, f: typeof fetch, r: typeof require, s: typeof std, o: typeof os }; }",
754        }))
755        .await
756        .unwrap();
757        for key in ["t", "f", "r", "s", "o"] {
758            assert_eq!(out.outputs.get(key), Some(&Value::from("undefined")), "{key}");
759        }
760    }
761
762    #[test]
763    fn runs_without_tokio_runtime_context() {
764        let n = node(serde_json::json!({ "code": "function main() { return { ok: true }; }" })).unwrap();
765        let out = futures::executor::block_on(n.run(NodeContext::for_test("c", pool()))).unwrap();
766        assert!(matches!(out, NodeOutcome::Completed(o) if o.outputs.get("ok") == Some(&Value::Bool(true))));
767    }
768
769    #[tokio::test]
770    async fn python_without_remote_sandbox() {
771        let err = run(serde_json::json!({
772            "code_language": "python3",
773            "code": "def main():\n    return {}",
774        }))
775        .await
776        .unwrap_err();
777        assert_eq!(err.kind, "missing_service");
778        assert!(err.message.contains("remote sandbox"));
779    }
780
781    #[derive(Debug)]
782    struct EchoSandbox;
783
784    impl RemoteSandbox for EchoSandbox {
785        fn execute<'a>(
786            &'a self,
787            language: &'a str,
788            _code: &'a str,
789            inputs: serde_json::Value,
790            _limits: CodeLimits,
791        ) -> BoxFuture<'a, Result<serde_json::Value, NodeError>> {
792            Box::pin(async move { Ok(serde_json::json!({ "language": language, "inputs": inputs })) })
793        }
794    }
795
796    #[tokio::test]
797    async fn python_with_remote_sandbox() {
798        let n = node(serde_json::json!({
799            "code_language": "python3",
800            "code": "def main(a):\n    return {}",
801            "variables": [{"variable": "a", "value_selector": ["start", "a"]}],
802        }))
803        .unwrap();
804        let services = Services::new().with::<Arc<dyn RemoteSandbox>>(Arc::new(EchoSandbox));
805        let ctx = NodeContext::for_test("c", pool()).with_services(Arc::new(services));
806        let out = n.run(ctx).await.unwrap();
807        let NodeOutcome::Completed(out) = out else { panic!("paused") };
808        assert_eq!(out.outputs.get("language"), Some(&Value::from("python3")));
809        assert_eq!(out.outputs.get("inputs"), Some(&Value::from_json(serde_json::json!({"a": 2}))));
810    }
811
812    #[test]
813    fn config_validation() {
814        assert!(node(serde_json::json!({ "code": "  " })).is_err());
815        assert!(node(serde_json::json!({ "code_language": "jinja2", "code": "x" })).is_err());
816        assert!(node(serde_json::json!({ "code_language": "js", "code": "x" })).is_ok());
817    }
818
819    #[test]
820    fn registers_kind() {
821        let mut r = Registry::new();
822        register(&mut r);
823        assert!(r.contains(KIND));
824    }
825}