AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(blocks): support large ExecuteCodeBlock variables and JavaScript NaN (#15129)
Bentlya7fe56d05b79
diff --git a/autogpt_platform/backend/backend/blocks/code_executor.py b/autogpt_platform/backend/backend/blocks/code_executor.pyindex 981506903a..a18958c61d 100644--- a/autogpt_platform/backend/backend/blocks/code_executor.py+++ b/autogpt_platform/backend/backend/blocks/code_executor.py@@ -136,6 +136,7 @@ class BaseE2BExecutorMixin:execution_context: Optional["ExecutionContext"] = None,extract_files: bool = False,envs: Optional[dict[str, str]] = None,+ input_files: Optional[dict[str, bytes]] = None,metadata: Optional[dict[str, str]] = None,):"""@@ -147,6 +148,8 @@ class BaseE2BExecutorMixin:Args:extract_files: If True and execution_context provided, extract filescreated/modified during execution and store to workspace.+ input_files: Files to write into the sandbox before the code+ runs, keyed by absolute path.""" # noqasandbox = Nonefiles: list[SandboxFileOutput] = []@@ -185,6 +188,9 @@ class BaseE2BExecutorMixin:for cmd in setup_commands:await sandbox.commands.run(cmd)+ for path, data in (input_files or {}).items():+ await sandbox.files.write(path, data)+# Capture timestamp before execution to scope file extractionstart_timestamp = Noneif extract_files:@@ -287,7 +293,13 @@ class ExecuteCodeBlock(Block, BaseE2BExecutorMixin):"variable with the same name (`{name}`) in your code. ""Values wired in from other blocks keep their type; default values set ""on this node come in as strings, so parse them in your code "- "if you need a number or other type."+ "if you need a number or other type. "+ "Values arrive exactly as given (any quotes, backslashes or "+ "unicode); in JavaScript, NaN and Infinity arrive as null. "+ "Up to 10 MB of JSON in total. Up to 64 KB it is also in the "+ "`AGPT_VARIABLES` env var (base64 JSON); above that it is in "+ "the JSON file named by `AGPT_VARIABLES_FILE`. For more than "+ "10 MB, put the data at a URL and download it in your code."),default_factory=dict,advanced=False,@@ -355,7 +367,11 @@ class ExecuteCodeBlock(Block, BaseE2BExecutorMixin):super().__init__(id="0b02b072-abe7-11ef-8372-fb5d162dd712",capability_kind="primitive",- description="Executes code in a sandbox environment with internet access.",+ description=(+ "Executes code in a sandbox environment with internet access. "+ "Python and JavaScript code can take up to 10 MB of input "+ "data through `variables`."+ ),categories={BlockCategory.DEVELOPER_TOOLS},input_schema=ExecuteCodeBlock.Input,output_schema=ExecuteCodeBlock.Output,@@ -386,13 +402,14 @@ class ExecuteCodeBlock(Block, BaseE2BExecutorMixin):**kwargs,) -> BlockOutput:try:- # Expose user-provided variables by passing them as a JSON env var and- # prepending a constant snippet that deserializes them into the runtime.- # Keeping the data in the env var (not the code string) avoids injection.- envs, prefix = build_variable_injection(+ # Expose user-provided variables through an env var (or, for large+ # payloads, a file) and prepend a constant snippet that loads them+ # into the runtime. Keeping the data out of the code string avoids+ # injection.+ injection = build_variable_injection(input_data.variables, input_data.language)- code = prefix + input_data.code+ code = injection.prefix + input_data.coderesults, text_output, stdout, stderr, _, files = await self.execute_code(api_key=credentials.api_key.get_secret_value(),@@ -404,7 +421,8 @@ class ExecuteCodeBlock(Block, BaseE2BExecutorMixin):dispose_sandbox=input_data.dispose_sandbox,execution_context=execution_context,extract_files=True,- envs=envs,+ envs=injection.envs,+ input_files=injection.files,metadata=SandboxMetadata.for_block(execution_context, "code", self.id, input_data.template_id).as_e2b(),diff --git a/autogpt_platform/backend/backend/blocks/code_executor_helpers.py b/autogpt_platform/backend/backend/blocks/code_executor_helpers.pyindex e9af108bd4..5096d4ef5d 100644--- a/autogpt_platform/backend/backend/blocks/code_executor_helpers.py+++ b/autogpt_platform/backend/backend/blocks/code_executor_helpers.py@@ -21,6 +21,12 @@ failure mode, not a hypothetical (see PR discussion). Base64 text is pureASCII with no backslashes or unicode escapes for any downstream text-handlinglayer to misinterpret, so it closes the whole bug class rather than special-casing individual malformed inputs.++Payloads over `MAX_ENV_PAYLOAD_BYTES` are written to a JSON file in the+sandbox instead, with its path in `AGPT_VARIABLES_FILE`. An env var can't+carry them: Linux caps a single env string at 128 KiB (MAX_ARG_STRLEN), and+the sandbox's env is inherited by every process the user's code starts, so an+oversized one makes each `subprocess`/`pip`/`!cmd` call fail with E2BIG."""import base64@@ -29,6 +35,8 @@ import keywordfrom enum import Enumfrom typing import Any+from pydantic import BaseModel+class ProgrammingLanguage(Enum):PYTHON = "python"@@ -38,28 +46,54 @@ class ProgrammingLanguage(Enum):JAVA = "java"-# Env var name used to carry the serialized variables into the sandbox.+# Env var carrying the base64-encoded JSON payload into the sandbox.VARIABLES_ENV_KEY = "AGPT_VARIABLES"+# Env var naming the JSON file that carries a payload too big for the env.+VARIABLES_FILE_ENV_KEY = "AGPT_VARIABLES_FILE"+# Outside the working directory, so it isn't picked up as an output file.+VARIABLES_FILE_PATH = "/tmp/agpt/variables.json"-# Cap the serialized payload to stay well under typical OS environment limits-# (which range from ~128 KB to a couple MB). Keeps failures clear instead of-# surfacing as cryptic sandbox startup errors.-MAX_VARIABLES_PAYLOAD_BYTES = 64 * 1024+# Largest JSON payload sent through the env var. Base64 grows it by a third,+# which keeps the env string under Linux's 128 KiB per-string limit.+MAX_ENV_PAYLOAD_BYTES = 64 * 1024+# Largest JSON payload accepted at all; bigger data belongs in a file the code+# downloads itself.+MAX_VARIABLES_PAYLOAD_BYTES = 10 * 1024 * 1024class UnsupportedLanguageError(ValueError):"""Raised when variable injection is requested for an unsupported language."""-# Constant prefixes. They reference only the env var (data), never user values.+class VariableInjection(BaseModel):+ """What the sandbox needs so the code sees `variables` as named values."""++ envs: dict[str, str] = {}+ """Env vars to set for the run."""+ files: dict[str, bytes] = {}+ """Files to write into the sandbox before the run, by absolute path."""+ prefix: str = ""+ """Constant code to prepend to the user's code."""+++# Constant prefixes. They read the payload from the file or env var named by+# the env (data), never from user values in the code string._PYTHON_PREFIX = ("import base64 as _agpt_b64, json as _agpt_json, os as _agpt_os\n"- "globals().update(_agpt_json.loads(_agpt_b64.b64decode("- f'_agpt_os.environ["{VARIABLES_ENV_KEY}"]).decode("utf-8")))\n'+ "globals().update(_agpt_json.loads("+ f'open(_agpt_os.environ["{VARIABLES_FILE_ENV_KEY}"], "rb").read() '+ f'if "{VARIABLES_FILE_ENV_KEY}" in _agpt_os.environ '+ f'else _agpt_b64.b64decode(_agpt_os.environ["{VARIABLES_ENV_KEY}"])))\n')+# `require` is missing in ES-module kernels, where `process.getBuiltinModule`+# (Node 22.3+) stands in for it._JAVASCRIPT_PREFIX = (- "Object.assign(globalThis, JSON.parse(Buffer.from("- f"process.env.{VARIABLES_ENV_KEY}, 'base64').toString('utf-8')));\n"+ "Object.assign(globalThis, JSON.parse("+ f"process.env.{VARIABLES_FILE_ENV_KEY} ? "+ "(typeof require === 'function' ? require('fs') : "+ "process.getBuiltinModule('fs'))"+ f".readFileSync(process.env.{VARIABLES_FILE_ENV_KEY}, 'utf-8') : "+ f"Buffer.from(process.env.{VARIABLES_ENV_KEY}, 'base64').toString('utf-8')));\n")_PREFIX_BY_LANGUAGE = {@@ -71,18 +105,19 @@ _PREFIX_BY_LANGUAGE = {def build_variable_injection(variables: dict[str, Any],language: ProgrammingLanguage,-) -> tuple[dict[str, str], str]:- """Build the env vars and code prefix needed to expose `variables`.+) -> VariableInjection:+ """Build the env vars, files and code prefix needed to expose `variables`.- Returns a tuple of:- - envs: env-var dict to pass to the sandbox (empty if no variables)- - prefix: code to prepend so the variables exist as named variables- (empty string if no variables)+ Up to `MAX_ENV_PAYLOAD_BYTES` the payload travels base64-encoded in the+ `AGPT_VARIABLES` env var, as it always has. Above that it is written to+ `VARIABLES_FILE_PATH` as plain JSON, named by `AGPT_VARIABLES_FILE`.- Raises UnsupportedLanguageError if `language` has no injection strategy.+ Raises UnsupportedLanguageError if `language` has no injection strategy,+ and ValueError for bad names, unserializable values or a payload over+ `MAX_VARIABLES_PAYLOAD_BYTES`."""if not variables:- return {}, ""+ return VariableInjection()prefix = _PREFIX_BY_LANGUAGE.get(language)if prefix is None:@@ -100,17 +135,32 @@ def build_variable_injection(raise ValueError(f"Variable value is not serializable for key(s): {', '.join(bad_keys)}") from e+ if language is ProgrammingLanguage.JAVASCRIPT and (+ "NaN" in serialized or "Infinity" in serialized+ ):+ # JSON.parse rejects the NaN/Infinity that json.dumps writes; turn them+ # into null, as JavaScript's own JSON.stringify does.+ serialized = json.dumps(json.loads(serialized, parse_constant=lambda _: None))serialized_bytes = serialized.encode("utf-8")- if len(serialized_bytes) > MAX_VARIABLES_PAYLOAD_BYTES:+ size = len(serialized_bytes)+ if size > MAX_VARIABLES_PAYLOAD_BYTES:raise ValueError(- "Variables payload is too large "- f"(max {MAX_VARIABLES_PAYLOAD_BYTES // 1024} KB). "- "Pass large data through a file or upstream block instead."+ f"Variables payload is too large ({size / 1024 / 1024:.1f} MB, "+ f"max {MAX_VARIABLES_PAYLOAD_BYTES // 1024 // 1024} MB). "+ "Put the data in a file or at a URL and read or download it from "+ "your code instead.")-- envs = {VARIABLES_ENV_KEY: base64.b64encode(serialized_bytes).decode("ascii")}- return envs, prefix+ if size > MAX_ENV_PAYLOAD_BYTES:+ return VariableInjection(+ envs={VARIABLES_FILE_ENV_KEY: VARIABLES_FILE_PATH},+ files={VARIABLES_FILE_PATH: serialized_bytes},+ prefix=prefix,+ )+ return VariableInjection(+ envs={VARIABLES_ENV_KEY: base64.b64encode(serialized_bytes).decode("ascii")},+ prefix=prefix,+ )def _validate_keys(variables: dict[str, Any]) -> None:diff --git a/autogpt_platform/backend/backend/blocks/code_executor_test.py b/autogpt_platform/backend/backend/blocks/code_executor_test.pyindex dfb377bb59..d3ede114f7 100644--- a/autogpt_platform/backend/backend/blocks/code_executor_test.py+++ b/autogpt_platform/backend/backend/blocks/code_executor_test.py@@ -23,12 +23,15 @@ from backend.blocks.code_executor import (ProgrammingLanguage,)from backend.blocks.code_executor_helpers import (+ MAX_ENV_PAYLOAD_BYTES,MAX_VARIABLES_PAYLOAD_BYTES,VARIABLES_ENV_KEY,+ VARIABLES_FILE_ENV_KEY,+ VARIABLES_FILE_PATH,UnsupportedLanguageError,build_variable_injection,)-from backend.executor.utils import ExecutionContext+from backend.data.execution import ExecutionContextdef _b64_json(variables: dict) -> str:@@ -76,13 +79,15 @@ async def _run(block: ExecuteCodeBlock, input_data: ExecuteCodeBlock.Input):class TestBuildVariableInjection:def test_empty_variables_returns_noop(self):"""No variables -> no env var, no prepended code (don't touch anything)."""- envs, prefix = build_variable_injection({}, ProgrammingLanguage.PYTHON)- assert envs == {}- assert prefix == ""+ injection = build_variable_injection({}, ProgrammingLanguage.PYTHON)+ assert injection.envs == {}+ assert injection.files == {}+ assert injection.prefix == ""def test_python_serializes_to_env_and_unpacks_to_globals(self):variables = {"x": 42, "name": "Blake", "items": [1, 2, 3]}- envs, prefix = build_variable_injection(variables, ProgrammingLanguage.PYTHON)+ injection = build_variable_injection(variables, ProgrammingLanguage.PYTHON)+ envs, prefix = injection.envs, injection.prefix# Data travels in the env var, JSON-encoded then base64-encoded.assert envs == {VARIABLES_ENV_KEY: _b64_json(variables)}@@ -98,9 +103,8 @@ class TestBuildVariableInjection:def test_javascript_serializes_to_env_and_unpacks_to_globalthis(self):variables = {"x": 42, "name": "Blake"}- envs, prefix = build_variable_injection(- variables, ProgrammingLanguage.JAVASCRIPT- )+ injection = build_variable_injection(variables, ProgrammingLanguage.JAVASCRIPT)+ envs, prefix = injection.envs, injection.prefixassert envs == {VARIABLES_ENV_KEY: _b64_json(variables)}assert "base64" in prefix@@ -112,7 +116,8 @@ class TestBuildVariableInjection:def test_malicious_value_cannot_break_out_of_code_channel(self):"""A value that looks like code stays inert: it's only ever JSON data."""variables = {"evil": "'); import os; os.system('rm -rf /'); ('"}- envs, prefix = build_variable_injection(variables, ProgrammingLanguage.PYTHON)+ injection = build_variable_injection(variables, ProgrammingLanguage.PYTHON)+ envs, prefix = injection.envs, injection.prefix# The dangerous string lives only in the env payload, never in the code.assert "os.system" not in prefixassert envs[VARIABLES_ENV_KEY] == _b64_json(variables)@@ -148,12 +153,21 @@ class TestBuildVariableInjection:with pytest.raises(ValueError, match="too large"):build_variable_injection(big, ProgrammingLanguage.PYTHON)+ def test_payload_over_the_env_limit_goes_to_a_file(self):+ variables = {"data": "x" * MAX_ENV_PAYLOAD_BYTES}+ injection = build_variable_injection(variables, ProgrammingLanguage.PYTHON)++ assert injection.envs == {VARIABLES_FILE_ENV_KEY: VARIABLES_FILE_PATH}+ assert json.loads(injection.files[VARIABLES_FILE_PATH]) == variables+ assert VARIABLES_FILE_ENV_KEY in injection.prefix+ assert "x" * 100 not in injection.prefix+def test_real_emoji_survives_sanitization_untouched(self):"""Properly-paired surrogates (real emoji) are valid Unicode and mustround-trip through the injection helper untouched."""variables = {"note": "Holidays \U0001f385\U0001f3fb"}- envs, _ = build_variable_injection(variables, ProgrammingLanguage.PYTHON)+ envs = build_variable_injection(variables, ProgrammingLanguage.PYTHON).envsassert _decode_env_payload(envs) == variables@@ -171,7 +185,7 @@ class TestBuildVariableInjection:there's nothing left for any downstream layer to misparse."""variables = {"note": "Holidays \U0001f385\U0001f3fb"}- envs, _ = build_variable_injection(variables, ProgrammingLanguage.PYTHON)+ envs = build_variable_injection(variables, ProgrammingLanguage.PYTHON).envspayload = envs[VARIABLES_ENV_KEY]assert "\\u" not in payload@@ -267,6 +281,47 @@ class TestExecuteCodeBlockRun:mock.assert_not_called()+class TestExecuteCodeWritesFiles:+ async def test_files_are_written_before_the_code_runs(self):+ block = ExecuteCodeBlock()+ calls: list[str] = []+ execution = MagicMock(error=None, results=[], text="")+ execution.logs.stdout, execution.logs.stderr = [], []++ async def write(*_):+ calls.append("write")++ async def command(*_):+ calls.append("setup")++ async def run_code(*_, **__):+ calls.append("run_code")+ return execution++ sandbox = MagicMock(sandbox_id="sb")+ sandbox.commands.run = AsyncMock(side_effect=command)+ sandbox.files.write = AsyncMock(side_effect=write)+ sandbox.run_code = AsyncMock(side_effect=run_code)+ with patch(+ "backend.blocks.code_executor.create_sandbox",+ AsyncMock(return_value=sandbox),+ ):+ await block.execute_code(+ api_key="k",+ code="print(1)",+ language=ProgrammingLanguage.PYTHON,+ setup_commands=["echo setup"],+ envs={VARIABLES_FILE_ENV_KEY: VARIABLES_FILE_PATH},+ input_files={VARIABLES_FILE_PATH: b"{}"},+ )++ assert calls == ["setup", "write", "run_code"]+ sandbox.files.write.assert_awaited_once_with(VARIABLES_FILE_PATH, b"{}")+ assert sandbox.run_code.await_args.kwargs["envs"] == {+ VARIABLES_FILE_ENV_KEY: VARIABLES_FILE_PATH+ }++class TestConnectToExistingSandbox:"""A sandbox id is caller input; under the platform key any id connects,so the box's stamped user decides whether it may be used."""diff --git a/autogpt_platform/backend/backend/blocks/code_executor_variables_test.py b/autogpt_platform/backend/backend/blocks/code_executor_variables_test.pynew file mode 100644index 0000000000..8b7538b1b8--- /dev/null+++ b/autogpt_platform/backend/backend/blocks/code_executor_variables_test.py@@ -0,0 +1,287 @@+"""End-to-end tests for how ExecuteCodeBlock hands `variables` to the code.++Only the sandbox SDK and output storage are replaced. The real block and+executor write files, pass env vars, and execute their prefix with real Python+and Node interpreters.+"""++import json+import math+import os+import shutil+import subprocess+import sys+import uuid+from pathlib import Path+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from e2b_code_interpreter.models import Execution, Logs++from backend.blocks.code_executor import (+ TEST_CREDENTIALS,+ TEST_CREDENTIALS_INPUT,+ ExecuteCodeBlock,+ ProgrammingLanguage,+)+from backend.blocks.code_executor_helpers import (+ MAX_ENV_PAYLOAD_BYTES,+ MAX_VARIABLES_PAYLOAD_BYTES,+ VARIABLES_FILE_ENV_KEY,+ build_variable_injection,+)+from backend.data.execution import ExecutionContext++PYTHON = ProgrammingLanguage.PYTHON+JAVASCRIPT = ProgrammingLanguage.JAVASCRIPT++_NODE = shutil.which("node")+_LANGUAGES = [+ PYTHON,+ pytest.param(+ JAVASCRIPT,+ marks=pytest.mark.skipif(_NODE is None, reason="node is not installed"),+ ),+]++TRICKY = {+ "quotes": "say \"hi\", it's 'fine'",+ "backslashes": "C:\\Users\\me\\new\\table \\u0041 \\\\ trailing\\",+ "newlines": "line 1\nline 2\r\n\ttabbed",+ "unicode": "🎅🏻 café 中文 Ελληνικά \u2028\u2029 \u0000",+ "json_text": json.dumps({"msg": 'he said "no"\\n', "emoji": "😀"}),+ "nested": {"list": ['"a"', "\\", None, True, 1.5, {"deep": "ünï"}]},+}++# A realistic AutoPilot payload: ~1 MB of notifications full of the same+# characters that are hard to escape.+LARGE = {+ "notifications": [+ {"id": i, "title": f'Note "{i}"', "body": "C:\\path ✓ 😀 " * 40}+ for i in range(1_800)+ ]+}+++class LocalSandbox:+ """Runs what the block would send to E2B, locally."""++ def __init__(self, workdir: Path, javascript_module: bool = False):+ self.workdir = workdir+ self.javascript_module = javascript_module+ self.sandbox_id = "local"+ self.calls: list[dict] = []+ self.input_files: dict[str, bytes] = {}+ self.create = AsyncMock(return_value=self)+ self.dispose = AsyncMock()+ self.files = MagicMock(write=AsyncMock(side_effect=self.write_file))+ self.commands = MagicMock(+ run=AsyncMock(return_value=MagicMock(stdout="2026-10-19T00:00:00"))+ )++ async def write_file(self, path: str, data: bytes):+ self.input_files[path] = data+ (self.workdir / Path(path).name).write_bytes(data)++ async def run_code(self, code: str, *, language: str, envs=None, **_):+ env = {**os.environ, **(envs or {})}+ for sandbox_path in self.input_files:+ local = self.workdir / Path(sandbox_path).name+ env = {k: str(local) if v == sandbox_path else v for k, v in env.items()}+ self.calls.append({"envs": envs or {}, "files": self.input_files.copy()})+ node_args = ["--input-type=module"] if self.javascript_module else []+ cmd = (+ [sys.executable, "-c", code]+ if language == PYTHON.value+ else [str(_NODE), *node_args, "-e", code]+ )+ proc = subprocess.run(+ cmd, env=env, capture_output=True, text=True, encoding="utf-8", timeout=20+ )+ if proc.returncode != 0:+ lines = proc.stderr.strip().splitlines()+ raise RuntimeError(next((ln for ln in lines if "Error" in ln), lines[-1]))+ return Execution(logs=Logs(stdout=[proc.stdout], stderr=[proc.stderr]))+++def _dump_code(language: ProgrammingLanguage, names: list[str]) -> str:+ if language is PYTHON:+ return f"import json\nprint(json.dumps({{k: globals()[k] for k in {names!r}}}))"+ return f"console.log(JSON.stringify({{{', '.join(names)}}}))"+++async def _run(+ tmp_path: Path,+ language,+ variables: dict,+ code: str | None = None,+ *,+ javascript_module: bool = False,+):+ block = ExecuteCodeBlock()+ sandbox = LocalSandbox(tmp_path, javascript_module)+ input_data = ExecuteCodeBlock.Input.model_validate(+ {+ "credentials": TEST_CREDENTIALS_INPUT,+ "language": language.value,+ "variables": variables,+ "code": code or _dump_code(language, list(variables)),+ }+ )+ context = ExecutionContext(user_id=str(uuid.uuid4()), graph_exec_id="gexec")+ with (+ patch(+ "backend.blocks.code_executor.create_sandbox",+ sandbox.create,+ ),+ patch("backend.blocks.code_executor.kill_sandbox", sandbox.dispose),+ patch(+ "backend.blocks.code_executor.extract_and_store_sandbox_files",+ AsyncMock(return_value=[]),+ ),+ ):+ outputs = dict(+ [+ item+ async for item in block.run(+ input_data, credentials=TEST_CREDENTIALS, execution_context=context+ )+ ]+ )+ return outputs, sandbox+++def _read_back(outputs: dict) -> dict:+ assert "error" not in outputs, outputs["error"]+ return json.loads(outputs["stdout_logs"])++[email protected]("language", _LANGUAGES)+async def test_tricky_values_reach_the_code_unchanged(tmp_path, language):+ """Quotes, backslashes, newlines, unicode and JSON-in-a-string must come+ out exactly as they went in. (Already true since #13576 base64-encoded the+ payload; kept as a guard.)"""+ outputs, _ = await _run(tmp_path, language, TRICKY)+ assert _read_back(outputs) == TRICKY++[email protected]("language", _LANGUAGES)+async def test_large_payload_reaches_the_code_unchanged(tmp_path, language):+ """A ~1 MB payload used to be refused at 64 KB."""+ assert len(json.dumps(LARGE).encode()) > 1_000_000+ outputs, sandbox = await _run(tmp_path, language, LARGE)+ assert _read_back(outputs) == LARGE+ # Too big for an env var: one env string over 128 KiB makes every+ # subprocess the user's code starts fail with E2BIG.+ assert all(len(v) < 100_000 for v in sandbox.calls[0]["envs"].values())++[email protected]("language", _LANGUAGES)+async def test_large_payload_path_is_exposed_to_the_code(tmp_path, language):+ code = (+ "import json, os\nprint(json.dumps(len(json.load(open(os.environ['AGPT_VARIABLES_FILE']))['notifications'])))"+ if language is PYTHON+ else "console.log(JSON.stringify(JSON.parse(require('fs').readFileSync(process.env.AGPT_VARIABLES_FILE, 'utf-8')).notifications.length))"+ )+ outputs, _ = await _run(tmp_path, language, LARGE, code)+ assert _read_back(outputs) == len(LARGE["notifications"])++[email protected]("large", [False, True])+async def test_non_finite_numbers_do_not_break_javascript(tmp_path, large):+ """JSON has no NaN/Infinity; Python's json writes them anyway and+ JavaScript's JSON.parse rejects them. Spreadsheet data is full of NaN."""+ if _NODE is None:+ pytest.skip("node is not installed")+ variables = {+ "rows": [1.5, math.nan, math.inf, -math.inf, {"nested": math.nan}],+ "text": "NaN Infinity -Infinity",+ "padding": "x" * (MAX_ENV_PAYLOAD_BYTES if large else 0),+ }+ outputs, _ = await _run(tmp_path, JAVASCRIPT, variables)+ # The same values JavaScript's own JSON.stringify would produce.+ assert _read_back(outputs) == {+ **variables,+ "rows": [1.5, None, None, None, {"nested": None}],+ }++[email protected]("large", [False, True])+async def test_non_finite_numbers_survive_in_python(tmp_path, large):+ variables = {+ "rows": [1.5, math.nan, math.inf, -math.inf],+ "padding": "x" * (MAX_ENV_PAYLOAD_BYTES if large else 0),+ }+ code = "print(repr(rows))"+ outputs, _ = await _run(tmp_path, PYTHON, variables, code)+ assert "error" not in outputs, outputs["error"]+ assert outputs["stdout_logs"].strip() == "[1.5, nan, inf, -inf]"+++async def test_small_payload_still_uses_the_env_var(tmp_path):+ """Code that reads AGPT_VARIABLES itself keeps working for every payload+ size that worked before."""+ code = (+ "import base64, json, os\n"+ "print(json.dumps(json.loads(base64.b64decode(os.environ['AGPT_VARIABLES']))))"+ )+ outputs, sandbox = await _run(tmp_path, PYTHON, TRICKY, code)+ assert _read_back(outputs) == TRICKY+ assert sandbox.calls[0]["files"] == {}++[email protected]("language", _LANGUAGES)+async def test_large_payload_does_not_break_subprocesses(tmp_path, language):+ code = (+ "import subprocess, sys\n"+ "subprocess.run([sys.executable, '-c', 'print(42)'], check=True)"+ if language is PYTHON+ else "console.log(require('child_process').execFileSync("+ "process.execPath, ['-e', 'console.log(42)'], {encoding: 'utf-8'}).trim())"+ )+ outputs, _ = await _run(tmp_path, language, LARGE, code)+ assert _read_back(outputs) == 42++[email protected](_NODE is None, reason="node is not installed")+async def test_large_payload_in_javascript_module(tmp_path):+ outputs, _ = await _run(tmp_path, JAVASCRIPT, LARGE, javascript_module=True)+ assert _read_back(outputs) == LARGE++[email protected]("language", [PYTHON, JAVASCRIPT])+ "size",+ [+ MAX_ENV_PAYLOAD_BYTES - 1,+ MAX_ENV_PAYLOAD_BYTES,+ MAX_ENV_PAYLOAD_BYTES + 1,+ MAX_VARIABLES_PAYLOAD_BYTES,+ ],+)+def test_serialized_size_boundaries(language, size):+ variables = {"data": "x" * (size - len(json.dumps({"data": ""})))}+ injection = build_variable_injection(variables, language)+ assert bool(injection.files) == (size > MAX_ENV_PAYLOAD_BYTES)+ assert (VARIABLES_FILE_ENV_KEY in injection.envs) == bool(injection.files)+ if injection.files:+ assert json.loads(next(iter(injection.files.values()))) == variables++[email protected]("language", _LANGUAGES)+async def test_over_limit_does_not_start_sandbox(tmp_path, language):+ variables = {"data": "x" * (MAX_VARIABLES_PAYLOAD_BYTES + 1)}+ outputs, sandbox = await _run(tmp_path, language, variables)+ assert "max 10 MB" in outputs["error"]+ sandbox.create.assert_not_awaited()+ sandbox.files.write.assert_not_awaited()+++async def test_file_upload_failure_stops_execution_and_disposes_sandbox(tmp_path):+ with patch.object(+ LocalSandbox, "write_file", AsyncMock(side_effect=OSError("upload failed"))+ ):+ outputs, sandbox = await _run(tmp_path, PYTHON, LARGE)+ assert outputs["error"] == "upload failed"+ assert sandbox.calls == []+ sandbox.dispose.assert_awaited_once_with(sandbox)diff --git a/docs/integrations/block-integrations/misc.md b/docs/integrations/block-integrations/misc.mdindex f1170994a6..0da6f55d9e 100644--- a/docs/integrations/block-integrations/misc.md+++ b/docs/integrations/block-integrations/misc.md@@ -315,13 +315,21 @@ This block uses the Reddit API via PRAW to edit the body text of a self-post you## Execute Code### What it is-Executes code in a sandbox environment with internet access.+Executes code in a sandbox environment with internet access. Python and JavaScript code can take up to 10 MB of input data through `variables`.### How it works<!-- MANUAL: how_it_works -->This block executes Python, JavaScript, or Bash code in an isolated E2B sandbox with internet access. Use setup_commands to install dependencies before running your code.The sandbox includes pip and npm pre-installed. Set timeout to limit execution time, and use dispose_sandbox to clean up after execution or keep the sandbox running for follow-up steps.++**Passing data in with `variables` (Python and JavaScript):** each key becomes a variable of the same name in your code, and its value arrives exactly as given, whatever quotes, backslashes or unicode it holds. The limit is 10 MB of JSON in total:++- Up to 64 KB, the data is also in the `AGPT_VARIABLES` environment variable as base64-encoded JSON.+- From 64 KB to 10 MB, it is written to a JSON file in the sandbox instead, and `AGPT_VARIABLES_FILE` holds that file's path. The variables are still set for you; read the file yourself only if you want the raw JSON.+- Over 10 MB, the block returns an error. Put the data at a URL (or in a file your code can fetch) and download it from your code.++JSON has no `NaN` or `Infinity`. In JavaScript they arrive as `null`, the same as `JSON.stringify` produces; Python keeps them as floats.<!-- END MANUAL -->### Inputs@@ -329,7 +337,7 @@ The sandbox includes pip and npm pre-installed. Set timeout to limit execution t| Input | Description | Type | Required ||-------|-------------|------|----------|| setup_commands | Shell commands to set up the sandbox before running the code. You can use `curl` or `git` to install your desired Debian based package manager. `pip` and `npm` are pre-installed. These commands are executed with `sh`, in the foreground. | List[str] | No |-| variables | Variables defined here can be used directly in your code. Each key (`variables_#_{name}`) is injected directly as a local variable with the same name (`{name}`) in your code. Values wired in from other blocks keep their type; default values set on this node come in as strings, so parse them in your code if you need a number or other type. | Dict[str, Any] | No |+| variables | Variables defined here can be used directly in your code. Each key (`variables_#_{name}`) is injected directly as a local variable with the same name (`{name}`) in your code. Values wired in from other blocks keep their type; default values set on this node come in as strings, so parse them in your code if you need a number or other type. Values arrive exactly as given (any quotes, backslashes or unicode); in JavaScript, NaN and Infinity arrive as null. Up to 10 MB of JSON in total. Up to 64 KB it is also in the `AGPT_VARIABLES` env var (base64 JSON); above that it is in the JSON file named by `AGPT_VARIABLES_FILE`. For more than 10 MB, put the data at a URL and download it in your code. | Dict[str, Any] | No || code | Code to execute in the sandbox | str | No || language | Programming language to execute | "python" \| "js" \| "bash" \| "r" \| "java" | No || timeout | Execution timeout in seconds | int | No |