Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/agent_env/task_step/task_steps/run_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -312,10 +313,21 @@ 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:
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__}"
)
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:
Expand Down
32 changes: 32 additions & 0 deletions tst/unit/task_step/test_run_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,38 @@ 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
@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=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


@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."""
Expand Down
Loading