Skip to content
Draft
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
17 changes: 12 additions & 5 deletions src/agent_env/task_step/task_steps/snapshot_agent_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import ClassVar, Optional

from agent_env.a2a_agent import A2AAgent
from agent_env.a2a_agent.object_transfer import TRANSFER_TIMEOUT_SECONDS
from agent_env.store.ids import derive_id, is_local_id, validate_local_id
from agent_env.task_step.context import TaskStepContext
from agent_env.entity.refs import EntityRef, RefRole
Expand Down Expand Up @@ -54,7 +55,11 @@ class SnapshotAgentStateTaskStep(TaskStep):
EntityRef.env("env_id"),
EntityRef.artifact("universe_artifact_id", artifact_type="environment_universe"),
)
DEFAULT_TIMEOUT_SECONDS: ClassVar[int] = 120
# A save moves the workspace, so it gets the budget of a call that moves objects.
DEFAULT_TIMEOUT_SECONDS: ClassVar[int] = TRANSFER_TIMEOUT_SECONDS
# to_dict once wrote the default into every stored step, so a stored 120 cannot be told apart
# from a chosen one. Reading it as unset lets those steps pick up the current default.
_LEGACY_DEFAULT_TIMEOUT_SECONDS: ClassVar[int] = 120

def __init__(
self,
Expand All @@ -65,7 +70,7 @@ def __init__(
agent_name: Optional[str] = None,
env_id: Optional[str] = None,
universe_artifact_id: Optional[str] = None,
timeout_seconds: int = DEFAULT_TIMEOUT_SECONDS,
timeout_seconds: Optional[int] = None,
depends_on: Optional[list[TaskStepDependency]] = None,
fail_task_on_error: bool = True,
):
Expand All @@ -90,19 +95,21 @@ def to_dict(self) -> dict:
base["agent_name"] = self.agent_name
base["env_id"] = self.env_id
base["universe_artifact_id"] = self.universe_artifact_id
base["timeout_seconds"] = self.timeout_seconds
if self.timeout_seconds is not None:
base["timeout_seconds"] = self.timeout_seconds
return base

@classmethod
def from_dict(cls, data: dict) -> SnapshotAgentStateTaskStep:
stored_timeout = data.get("timeout_seconds")
return cls(
**cls._base_from_dict(data),
artifact_id=data.get("artifact_id"),
prompt_id=data["prompt_id"],
agent_name=data.get("agent_name"),
env_id=data.get("env_id"),
universe_artifact_id=data.get("universe_artifact_id"),
timeout_seconds=data.get("timeout_seconds", cls.DEFAULT_TIMEOUT_SECONDS),
timeout_seconds=None if stored_timeout == cls._LEGACY_DEFAULT_TIMEOUT_SECONDS else stored_timeout,
)

@property
Expand Down Expand Up @@ -152,7 +159,7 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext:
agent_name=self.agent_name,
a2a_context_id=a2a_context_id,
artifact_id=self._resolve_artifact_id(context),
timeout_seconds=self.timeout_seconds,
timeout_seconds=self.DEFAULT_TIMEOUT_SECONDS if self.timeout_seconds is None else self.timeout_seconds,
sandbox_type=deployed.sandbox_type,
)

Expand Down
43 changes: 43 additions & 0 deletions tst/unit/task_step/test_snapshot_agent_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,46 @@ def test_a_stored_step_without_the_artifact_id_key_loads():
del data["artifact_id"]

assert mod.SnapshotAgentStateTaskStep.from_dict(data).artifact_id is None


async def _waited_for(monkeypatch, step) -> float:
waited: list[float] = []

async def _capture(*, timeout_seconds, artifact_id, **_):
waited.append(timeout_seconds)
return agent_state_capture.WorkspaceCapture(
universe_id=artifact_id, universe_version=1, bundle_object_url="fake://b/w", capture_prefix="fake://b/",
)

monkeypatch.setattr(agent_state_capture, "capture_workspace", _capture)
await step.execute(_run_ctx("inst-a"))
(seconds,) = waited
return seconds


@pytest.mark.asyncio
async def test_a_step_built_without_a_timeout_omits_it_and_waits_the_transfer_budget(monkeypatch):
stored = _per_run_step().to_dict()

assert "timeout_seconds" not in stored
assert await _waited_for(monkeypatch, mod.SnapshotAgentStateTaskStep.from_dict(stored)) == 600


@pytest.mark.asyncio
async def test_a_stored_step_carrying_the_old_default_waits_the_transfer_budget(monkeypatch):
stored = {**_per_run_step().to_dict(), "timeout_seconds": 120}

step = mod.SnapshotAgentStateTaskStep.from_dict(stored)

assert "timeout_seconds" not in step.to_dict()
assert await _waited_for(monkeypatch, step) == 600


@pytest.mark.asyncio
async def test_a_chosen_timeout_round_trips_and_bounds_the_save(monkeypatch):
stored = mod.SnapshotAgentStateTaskStep(
id="t-agent-snapshot", version=None, artifact_id=None, prompt_id="main", agent_name="agent", timeout_seconds=45,
).to_dict()

assert stored["timeout_seconds"] == 45
assert await _waited_for(monkeypatch, mod.SnapshotAgentStateTaskStep.from_dict(stored)) == 45
Loading