From b526cdd5f31b9619dc03cb942103aebdd5172115 Mon Sep 17 00:00:00 2001 From: Vijay Bharadwaj Date: Mon, 9 Nov 2026 18:05:13 +0000 Subject: [PATCH 1/2] feat(run_code): let a run's step_overrides set run_code args A run's step_overrides now reach run_code: `step_overrides[].args` is merged over the step's stored args, the run's keys winning, the same way run_container_unit_tests_verifier adds overridden env_vars to its stored ones. One stored task can then be run with per-run script inputs instead of a new task version per input. An args override that isn't an object is refused before the step touches a sandbox. Co-Authored-By: Claude Opus 5.5 --- .../task_step/task_steps/run_code.py | 17 ++++++++-- tst/unit/task_step/test_run_code.py | 31 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/agent_env/task_step/task_steps/run_code.py b/src/agent_env/task_step/task_steps/run_code.py index 6695a0e18..344c2e135 100644 --- a/src/agent_env/task_step/task_steps/run_code.py +++ b/src/agent_env/task_step/task_steps/run_code.py @@ -181,12 +181,13 @@ def preflight(self) -> list[str]: return [] async def execute(self, context: TaskStepContext) -> TaskStepContext: + args = self._args_for_run(context) script_file, files = await asyncio.to_thread(self._load_script) target = await self._resolve_target(context) run_dir = target.run_dir(self.id) try: - await self._stage(target, run_dir, files, self._build_input(context, target)) + await self._stage(target, run_dir, files, self._build_input(context, target, args)) await self._run(target, run_dir, script_file) result = await self._read_output(target, run_dir) finally: @@ -312,10 +313,20 @@ def _load_script(self) -> tuple[str, dict[str, bytes]]: ) return (script_file, files) - def _build_input(self, context: TaskStepContext, target: _ExecTarget) -> dict: + def _args_for_run(self, context: TaskStepContext) -> dict: + override = self.step_param_overrides(context).get("args") + if override is None: + return self.args + if not isinstance(override, dict): + raise RunCodeValidationError( + f"run_code '{self.id}': step_overrides 'args' must be an object, got {type(override).__name__}" + ) + return {**self.args, **override} + + def _build_input(self, context: TaskStepContext, target: _ExecTarget, args: dict) -> dict: """Build the {args, results} input passed to the script, and its env's endpoints on an env's host.""" script_input = { - "args": self.args, + "args": args, "results": context.metadata.get("script_results", {}), } if self.env_id is not None: diff --git a/tst/unit/task_step/test_run_code.py b/tst/unit/task_step/test_run_code.py index a972dd17a..04b05a5a1 100644 --- a/tst/unit/task_step/test_run_code.py +++ b/tst/unit/task_step/test_run_code.py @@ -375,6 +375,37 @@ async def test_a_script_in_an_agent_gets_no_env(): assert set(_written_input(sandbox)) == {"args", "results"} +def _run_overrides(step_params): + return _make_context(metadata={"user_overrides": {"step_params": step_params}}) + + +@pytest.mark.asyncio +async def test_a_runs_args_override_is_merged_over_the_stored_args(): + step = _step(args={"mode": "stored", "keep": 1}) + with _sandbox([(0, "", ""), (0, "2", ""), (0, "{}", "")]) as sandbox: + await step.execute(_run_overrides({"run-code-1": {"args": {"mode": "run", "before": [1, 2]}}})) + + assert _written_input(sandbox)["args"] == {"mode": "run", "keep": 1, "before": [1, 2]} + assert step.args == {"mode": "stored", "keep": 1} + + +@pytest.mark.asyncio +async def test_an_args_override_for_another_step_leaves_the_stored_args(): + with _sandbox([(0, "", ""), (0, "2", ""), (0, "{}", "")]) as sandbox: + await _step(args={"mode": "stored"}).execute(_run_overrides({"other-step": {"args": {"mode": "run"}}})) + + assert _written_input(sandbox)["args"] == {"mode": "stored"} + + +@pytest.mark.asyncio +async def test_an_args_override_that_is_not_an_object_is_refused_before_the_sandbox_is_touched(): + with _sandbox([(0, "", ""), (0, "2", ""), (0, "{}", "")]) as sandbox: + with pytest.raises(RunCodeValidationError, match="'args' must be an object, got list"): + await _step().execute(_run_overrides({"run-code-1": {"args": ["mode"]}})) + + assert not sandbox.exec_with_output.await_args_list + + @pytest.mark.asyncio async def test_runs_of_one_step_on_this_machine_stage_into_dirs_of_their_own(tmp_path): """Local sandboxes share this machine's /tmp: a run of the same step on another must not clear this one's files.""" From e072350f8723d8e6cbd1e7b611946f403e0d0263 Mon Sep 17 00:00:00 2001 From: Vijay Bharadwaj Date: Mon, 9 Nov 2026 19:14:59 +0000 Subject: [PATCH 2/2] fix(run_code): refuse an explicit null args override Co-Authored-By: Claude Opus 5.5 --- src/agent_env/task_step/task_steps/run_code.py | 5 +++-- tst/unit/task_step/test_run_code.py | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/agent_env/task_step/task_steps/run_code.py b/src/agent_env/task_step/task_steps/run_code.py index 344c2e135..0923ee2b6 100644 --- a/src/agent_env/task_step/task_steps/run_code.py +++ b/src/agent_env/task_step/task_steps/run_code.py @@ -314,9 +314,10 @@ def _load_script(self) -> tuple[str, dict[str, bytes]]: return (script_file, files) def _args_for_run(self, context: TaskStepContext) -> dict: - override = self.step_param_overrides(context).get("args") - if override is None: + overrides = self.step_param_overrides(context) + if "args" not in overrides: return self.args + override = overrides["args"] if not isinstance(override, dict): raise RunCodeValidationError( f"run_code '{self.id}': step_overrides 'args' must be an object, got {type(override).__name__}" diff --git a/tst/unit/task_step/test_run_code.py b/tst/unit/task_step/test_run_code.py index 04b05a5a1..d491a6521 100644 --- a/tst/unit/task_step/test_run_code.py +++ b/tst/unit/task_step/test_run_code.py @@ -398,10 +398,11 @@ async def test_an_args_override_for_another_step_leaves_the_stored_args(): @pytest.mark.asyncio -async def test_an_args_override_that_is_not_an_object_is_refused_before_the_sandbox_is_touched(): +@pytest.mark.parametrize("override, kind", [(["mode"], "list"), (None, "NoneType")]) +async def test_an_args_override_that_is_not_an_object_is_refused_before_the_sandbox_is_touched(override, kind): with _sandbox([(0, "", ""), (0, "2", ""), (0, "{}", "")]) as sandbox: - with pytest.raises(RunCodeValidationError, match="'args' must be an object, got list"): - await _step().execute(_run_overrides({"run-code-1": {"args": ["mode"]}})) + with pytest.raises(RunCodeValidationError, match=f"'args' must be an object, got {kind}"): + await _step().execute(_run_overrides({"run-code-1": {"args": override}})) assert not sandbox.exec_with_output.await_args_list