diff --git a/src/agent_env/a2a_agent/clock.py b/src/agent_env/a2a_agent/clock.py index 469c590e4..f03f55579 100644 --- a/src/agent_env/a2a_agent/clock.py +++ b/src/agent_env/a2a_agent/clock.py @@ -136,6 +136,22 @@ def torn_down(context: TaskStepContext, agent: DeployedAgent) -> bool: return bool(agent.sandbox_id) and agent.sandbox_id in (context.metadata.get(TORN_DOWN_KEY) or ()) +def _deploy_step_of(env: DeployedEnv) -> Optional[str]: + return (env.metadata or {}).get("deploy_step_id") + + +def on_deployment(agent: DeployedAgent, env: DeployedEnv) -> bool: + """Whether ``env`` is the deployment of its env that ``deploy_agent`` bound ``agent`` to, so a later deployment of + the same env never takes it over; any deployment is when it recorded none, as for an agent agent-env didn't deploy.""" + if env.env_id not in agent.env_bindings: + return True + return agent.env_bindings[env.env_id] == _deploy_step_of(env) + + +def _agent_deployment(context: TaskStepContext, agent: DeployedAgent, env_id: str) -> Optional[DeployedEnv]: + return next((d for d in context.deployed_envs if d.env_id == env_id and on_deployment(agent, d)), None) + + def clock_env(agent: DeployedAgent) -> Optional[str]: """The one env whose clock ``agent`` follows: the first it's registered with. Fixed by the agent alone, so two steps arming two of its envs' clocks at once never both take its one follower.""" @@ -148,10 +164,13 @@ async def follow_in(context: TaskStepContext, agent: DeployedAgent, env: Deploye its follower while that gateway still waits for it, is reported and skipped (``follows_another_env``).""" if env.env_id != clock_env(agent): return {"synced": False, "reason": "follows_another_env", "follows": clock_env(agent)} + if not on_deployment(agent, env): + return {"synced": False, "reason": "bound_to_another_deployment", "bound_to": agent.env_bindings[env.env_id]} outcome = await follow(agent, env, env_get_time_url=env_get_time_url, timezone=timezone, timeout=timeout) if outcome["synced"]: context.metadata.setdefault(FOLLOWING_KEY, {})[_slot(agent.agent_name)] = { - "agent": agent.agent_name, "env_id": env.env_id, "follower": outcome["follower"]} + "agent": agent.agent_name, "env_id": env.env_id, "deploy_step_id": _deploy_step_of(env), + "follower": outcome["follower"]} return outcome @@ -160,10 +179,11 @@ def summary(outcome: dict) -> dict: return {key: outcome[key] for key in ("synced", "reason", "follower", "follows") if key in outcome} -def _armed(context: TaskStepContext, env_id: str) -> Optional[dict]: - """The configuration of the last ``sync_env_clock`` that armed ``env_id``'s clock in this run, if any.""" +def _armed(context: TaskStepContext, env: DeployedEnv) -> Optional[dict]: + """The configuration of the last ``sync_env_clock`` that armed this deployment's clock in this run, if any.""" return next((c for c in reversed(context.metadata.get("clock_configurations") or []) - if c.get("env_id") == env_id and "env_get_time_url" in c), None) + if c.get("env_id") == env.env_id and "env_get_time_url" in c + and c.get("deploy_step_id", _deploy_step_of(env)) == _deploy_step_of(env)), None) async def follow_armed_clocks(context: TaskStepContext, agent: DeployedAgent, *, @@ -173,10 +193,8 @@ async def follow_armed_clocks(context: TaskStepContext, agent: DeployedAgent, *, that step's ``require_agent_clock``.""" outcomes = [] for env_id in agent.env_ids: - if (armed := _armed(context, env_id)) is None: - continue - env = next((d for d in context.deployed_envs if d.env_id == env_id), None) - if env is None or gateway_url_of(env) is None: + env = _agent_deployment(context, agent, env_id) + if env is None or gateway_url_of(env) is None or (armed := _armed(context, env)) is None: continue outcome = await follow_in(context, agent, env, env_get_time_url=armed.get("env_get_time_url"), timezone=armed.get("timezone"), timeout=timeout) @@ -197,7 +215,8 @@ async def retire_followers(context: TaskStepContext, agent_names, *, timeout: fl for name in dict.fromkeys(agent_names): if (record := following.get(_slot(name))) is None: continue - env = next((d for d in context.deployed_envs if d.env_id == record["env_id"]), None) + env = next((d for d in context.deployed_envs if d.env_id == record["env_id"] + and record.get("deploy_step_id", _deploy_step_of(d)) == _deploy_step_of(d)), None) if env is None: continue if not await _retire(env, record["follower"], timeout=timeout): diff --git a/src/agent_env/task_step/context.py b/src/agent_env/task_step/context.py index 32e7ac7a9..002a68d71 100644 --- a/src/agent_env/task_step/context.py +++ b/src/agent_env/task_step/context.py @@ -130,6 +130,8 @@ class DeployedAgent: installed_container: str | None = None # The envs deploy_agent registered with it, by id. env_ids: list[str] = field(default_factory=list) + # Per env id, the deploy_env step whose deployment deploy_agent bound it to; an env it isn't in binds it to none. + env_bindings: dict[str, str | None] = field(default_factory=dict) # The token its extension routes take, from the deploy that started it; None for an agent agent-env didn't start. control_token: str | None = None @@ -148,6 +150,7 @@ def from_dict(cls, data: dict) -> DeployedAgent: on_host=bool(data.get("on_host", False)), installed_container=data.get("installed_container"), env_ids=list(data.get("env_ids") or []), + env_bindings=dict(data.get("env_bindings") or {}), control_token=data.get("control_token"), ) diff --git a/src/agent_env/task_step/task_steps/deploy_agent.py b/src/agent_env/task_step/task_steps/deploy_agent.py index 6e7b513c5..e08d9dd7c 100644 --- a/src/agent_env/task_step/task_steps/deploy_agent.py +++ b/src/agent_env/task_step/task_steps/deploy_agent.py @@ -551,6 +551,8 @@ async def _execute(self, context: TaskStepContext, deployed_agents: list) -> Tas role=self.role, network_policy=deployed.network_policy, env_ids=list(self.env_ids), + env_bindings={env_id: (d.metadata or {}).get("deploy_step_id") + for env_id, d in env_deployments.items() if d is not None}, control_token=deployed.control_token, )) # An agent deployed while a clock is armed follows it, as sync_env_clock had every agent then. diff --git a/src/agent_env/task_step/task_steps/load_artifact.py b/src/agent_env/task_step/task_steps/load_artifact.py index 1762baaf0..96657cbb1 100644 --- a/src/agent_env/task_step/task_steps/load_artifact.py +++ b/src/agent_env/task_step/task_steps/load_artifact.py @@ -24,6 +24,7 @@ from agent_env.entity.scopes import FieldScope from agent_env.store.base import NotFoundError from agent_env.task_step.task_step import TaskStep, TaskStepDependency +from agent_env.task_step.task_steps.deploy_agent import bound_deployment from agent_env.utils.paths import validate_relative_filename logger = logging.getLogger(__name__) @@ -328,30 +329,6 @@ def _resolve_step_artifact(context: TaskStepContext, step_id: str) -> Optional[d f"this load step." ) - @staticmethod - def _deploy_step_of(deployed_env) -> Optional[str]: - return (deployed_env.metadata or {}).get("deploy_step_id") - - def _deployed_env(self, context: TaskStepContext): - """The deployment of `env_id` this step loads into, or None if it isn't deployed.""" - matches = [d for d in context.deployed_envs if d.env_id == self.env_id] - if self.env_step_id is not None: - # A filter, not a hint: an unmatched id is an error, never a substitute. - named = [d for d in matches if self._deploy_step_of(d) == self.env_step_id] - if matches and not named: - raise RuntimeError( - f"load_artifact '{self.id}': env_step_id '{self.env_step_id}' names no deployment of " - f"env '{self.env_id}' (deployed by: {[self._deploy_step_of(d) for d in matches]})" - ) - matches = named - if len(matches) > 1: - raise RuntimeError( - f"load_artifact '{self.id}': {len(matches)} deployments of env '{self.env_id}' " - f"(deployed by: {[self._deploy_step_of(d) for d in matches]}). Set env_step_id " - "to the deploy_env step this load should target." - ) - return matches[0] if matches else None - def _resolve_inputs(self, context: TaskStepContext) -> _LoadInputs: """Resolve ``(artifacts, downloads, destination_path)``, honoring per-run ``step_overrides``. An override wins over the stored value; an explicit @@ -445,7 +422,7 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: deployed = None if self.env_id is not None: - deployed = self._deployed_env(context) + deployed = bound_deployment(context, self.env_id, env_step_id=self.env_step_id, step_id=self.id) if deployed is None: raise StepContextError(f"Env '{self.env_id}' not found in context.deployed_envs") diff --git a/src/agent_env/task_step/task_steps/modify_env_tool_access.py b/src/agent_env/task_step/task_steps/modify_env_tool_access.py index 4e71d2f21..f45e5db09 100644 --- a/src/agent_env/task_step/task_steps/modify_env_tool_access.py +++ b/src/agent_env/task_step/task_steps/modify_env_tool_access.py @@ -10,6 +10,7 @@ from agent_env.task_step.context import StepContextError, TaskStepContext from agent_env.entity.refs import EntityRef from agent_env.task_step.task_step import TaskStep, TaskStepDependency +from agent_env.task_step.task_steps.deploy_agent import bound_deployment logger = logging.getLogger(__name__) @@ -26,6 +27,7 @@ def __init__( action: str, role: str, tools: list[str], + env_step_id: Optional[str] = None, depends_on: Optional[list[TaskStepDependency]] = None, fail_task_on_error: bool = True, ): @@ -37,6 +39,7 @@ def __init__( if not isinstance(tools, list) or not all(isinstance(t, str) for t in tools): raise ValueError("tools must be a list of strings") self.env_id = env_id + self.env_step_id = env_step_id self.action = action self.role = role self.tools = tools @@ -44,6 +47,7 @@ def __init__( def to_dict(self) -> dict: base = super().to_dict() base["env_id"] = self.env_id + base["env_step_id"] = self.env_step_id base["action"] = self.action base["role"] = self.role base["tools"] = self.tools @@ -54,13 +58,14 @@ def from_dict(cls, data: dict) -> ModifyEnvToolAccessStep: return cls( **cls._base_from_dict(data), env_id=data["env_id"], + env_step_id=data.get("env_step_id"), action=data["action"], role=data["role"], tools=data["tools"], ) async def execute(self, context: TaskStepContext) -> TaskStepContext: - deployed = next((d for d in context.deployed_envs if d.env_id == self.env_id), None) + deployed = bound_deployment(context, self.env_id, env_step_id=self.env_step_id, step_id=self.id) if deployed is None: raise StepContextError(f"Env '{self.env_id}' not found in context.deployed_envs") body = await deployed.invoke(_TOOL_ACCESS_URIS[self.action], self.action, {"role": self.role, "tools": self.tools}) diff --git a/src/agent_env/task_step/task_steps/register_env_triggers.py b/src/agent_env/task_step/task_steps/register_env_triggers.py index 6229a59e4..bb9fe330c 100644 --- a/src/agent_env/task_step/task_steps/register_env_triggers.py +++ b/src/agent_env/task_step/task_steps/register_env_triggers.py @@ -26,6 +26,7 @@ from agent_env.task_step.context import StepContextError, TaskStepContext from agent_env.entity.refs import EntityRef from agent_env.task_step.task_step import TaskStep, TaskStepDependency +from agent_env.task_step.task_steps.deploy_agent import bound_deployment logger = logging.getLogger(__name__) @@ -68,6 +69,7 @@ def __init__( executor_hours: Optional[dict[str, dict]] = None, await_fired: Optional[list[str]] = None, await_timeout_seconds: float = 120, + env_step_id: Optional[str] = None, depends_on: Optional[list[TaskStepDependency]] = None, fail_task_on_error: bool = True, ): @@ -81,6 +83,7 @@ def __init__( or await_timeout_seconds <= 0): raise ValueError("await_timeout_seconds must be a positive number") self.env_id = env_id + self.env_step_id = env_step_id self.triggers = triggers self.watch_roles = watch_roles self.executor_agent_name = executor_agent_name @@ -92,6 +95,7 @@ def __init__( def to_dict(self) -> dict: base = super().to_dict() base["env_id"] = self.env_id + base["env_step_id"] = self.env_step_id base["triggers"] = self.triggers base["watch_roles"] = self.watch_roles base["executor_agent_name"] = self.executor_agent_name @@ -106,6 +110,7 @@ def from_dict(cls, data: dict) -> RegisterEnvTriggersStep: return cls( **cls._base_from_dict(data), env_id=data["env_id"], + env_step_id=data.get("env_step_id"), triggers=data["triggers"], watch_roles=data.get("watch_roles"), executor_agent_name=data.get("executor_agent_name"), @@ -129,7 +134,7 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: problems = self.preflight() if problems: raise RuntimeError("; ".join(problems)) - deployed = next((d for d in context.deployed_envs if d.env_id == self.env_id), None) + deployed = bound_deployment(context, self.env_id, env_step_id=self.env_step_id, step_id=self.id) if deployed is None: raise StepContextError(f"Env '{self.env_id}' not found in context.deployed_envs") body: dict = {"triggers": self.triggers} diff --git a/src/agent_env/task_step/task_steps/sync_env_clock.py b/src/agent_env/task_step/task_steps/sync_env_clock.py index d3f2a0777..53d6c1f70 100644 --- a/src/agent_env/task_step/task_steps/sync_env_clock.py +++ b/src/agent_env/task_step/task_steps/sync_env_clock.py @@ -30,6 +30,7 @@ from agent_env.task_step.context import StepContextError, TaskStepContext from agent_env.entity.refs import EntityRef from agent_env.task_step.task_step import TaskStep, TaskStepDependency +from agent_env.task_step.task_steps.deploy_agent import bound_deployment if TYPE_CHECKING: from agent_env.env.legacy_protocol import BackingServer @@ -75,6 +76,7 @@ def __init__( virtual_time: str, virtual_seconds_per_real_second: float = 1.0, tolerate_missing_sync_time: bool = True, + env_step_id: Optional[str] = None, depends_on: Optional[list[TaskStepDependency]] = None, fail_task_on_error: bool = True, timeout_seconds: int = 30, @@ -94,6 +96,7 @@ def __init__( if not isinstance(virtual_time, str): raise ValueError("virtual_time must be an RFC3339 string") self.env_id = env_id + self.env_step_id = env_step_id self.virtual_time = virtual_time self.virtual_seconds_per_real_second = virtual_seconds_per_real_second self.tolerate_missing_sync_time = tolerate_missing_sync_time @@ -105,6 +108,7 @@ def __init__( def to_dict(self) -> dict: base = super().to_dict() base["env_id"] = self.env_id + base["env_step_id"] = self.env_step_id base["virtual_time"] = self.virtual_time base["virtual_seconds_per_real_second"] = self.virtual_seconds_per_real_second base["tolerate_missing_sync_time"] = self.tolerate_missing_sync_time @@ -120,6 +124,7 @@ def from_dict(cls, data: dict) -> SyncEnvClockTaskStep: return cls( **cls._base_from_dict(data), env_id=data["env_id"], + env_step_id=data.get("env_step_id"), virtual_time=data["virtual_time"], virtual_seconds_per_real_second=data.get("virtual_seconds_per_real_second", 1.0), tolerate_missing_sync_time=data.get("tolerate_missing_sync_time", True), @@ -139,7 +144,7 @@ async def _backing_servers(self, deployed) -> list[BackingServer]: async def execute(self, context: TaskStepContext) -> TaskStepContext: from agent_env.env.env import gateway_url_of - deployed = next((d for d in context.deployed_envs if d.env_id == self.env_id), None) + deployed = bound_deployment(context, self.env_id, env_step_id=self.env_step_id, step_id=self.id) if deployed is None: raise StepContextError(f"Env '{self.env_id}' not found in context.deployed_envs") try: @@ -175,6 +180,7 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: agents = await self._follow_agents(context, deployed, env_get_time_url) configuration = { "step_id": self.id, "env_id": self.env_id, + "deploy_step_id": (deployed.metadata or {}).get("deploy_step_id"), "virtual_time": self.virtual_time, "virtual_seconds_per_real_second": self.virtual_seconds_per_real_second, "env_get_time_url": env_get_time_url, "synced": synced, "skipped": skipped, "agents": agents, "require_agent_clock": self.require_agent_clock, @@ -194,12 +200,13 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: async def _follow_agents(self, context: TaskStepContext, deployed, env_get_time_url: Optional[str]) -> list[dict]: """Have every agent registered with the env follow its clock (``a2a_agent.clock.follow``): one that can't is reported and skipped, or fails the step under ``require_agent_clock``.""" - from agent_env.a2a_agent.clock import follow_in, torn_down + from agent_env.a2a_agent.clock import follow_in, on_deployment, torn_down outcomes = [] for agent in context.deployed_agents: - if self.env_id not in (agent.env_ids or []) or torn_down(context, agent): - continue # not the env's, or taken down by teardown_sandboxes: nothing to follow + mine = self.env_id in (agent.env_ids or []) and on_deployment(agent, deployed) + if not mine or torn_down(context, agent): + continue # not this deployment's, or taken down by teardown_sandboxes: nothing to follow outcome = await follow_in(context, agent, deployed, env_get_time_url=env_get_time_url, timezone=self.timezone, timeout=self.timeout_seconds) if not outcome["synced"]: diff --git a/tst/unit/task_step/agent_clock_test.py b/tst/unit/task_step/agent_clock_test.py index 595ee7bfc..551ba0cd0 100644 --- a/tst/unit/task_step/agent_clock_test.py +++ b/tst/unit/task_step/agent_clock_test.py @@ -3,6 +3,7 @@ one that doesn't is reported and skipped, even when the env's servers must follow (tolerate_missing_sync_time false), or fails the step under require_agent_clock; and an agent deploy_agent deploys while the clock is armed follows the same way.""" +import dataclasses import json from types import SimpleNamespace @@ -22,9 +23,10 @@ "request": {"required": ["env_get_time_url"], "optional": ["timezone", "watch_url", "name", "read_token"]}} -def _agent(name="solver", sync_time=SYNC_TIME, env_ids=("e1",)) -> DeployedAgent: +def _agent(name="solver", sync_time=SYNC_TIME, env_ids=("e1",), env_step_id=None) -> DeployedAgent: extensions = [{"uri": EXT_CLOCK_URI, "params": {"endpoint": "/ext/clock", "methods": {"sync_time": sync_time}}}] return DeployedAgent(agent_name=name, api_url=AGENT_URL, a2a_url=AGENT_URL, env_ids=list(env_ids), + env_bindings={env_id: env_step_id for env_id in env_ids} if env_step_id else {}, a2a_card={"capabilities": {"extensions": extensions if sync_time else []}}, control_token=AGENT_CONTROL_TOKEN) @@ -219,7 +221,7 @@ async def nothing(*a, **k): assert [(r.method, str(r.url), json.loads(r.content)) for r in sent] == [ ("DELETE", f"{_CARD}/clock/followers", {"name": "agent-solver"})] assert context.metadata[agent_clock.FOLLOWING_KEY] == { - "bystander": {"agent": "bystander", "env_id": "e1", "follower": "agent-bystander"}} + "bystander": {"agent": "bystander", "env_id": "e1", "deploy_step_id": None, "follower": "agent-bystander"}} assert agent_clock.followed_env(context, _agent()) is None @@ -323,3 +325,120 @@ async def test_a_later_sync_leaves_an_agent_teardown_took_down_alone(monkeypatch assert [a["agent"] for a in context.metadata["clock_configurations"][0]["agents"]] == ["kept"] assert not any("agent-gone" in (r.content or b"").decode() for r in sent) + + +async def _two_deployments(*agents) -> TaskStepContext: + """Two deployments of e1, by deploy_env steps deploy-a and deploy-b, each at a card URL of its own.""" + from agent_env.env.gateway.constants import WELL_KNOWN_PATH + + card = await _composed_card({"mcp-email": [_CLOCK_EXT]}) + envs = [dataclasses.replace(_carded(card), sandbox_id=f"sb-{lane}", + metadata={"deploy_step_id": f"deploy-{lane}"}, + environment_card_url=f"https://sandbox.example/sb-{lane}{WELL_KNOWN_PATH}") + for lane in ("a", "b")] + return TaskStepContext(deployed_envs=envs, deployed_agents=list(agents)) + + +@pytest.mark.asyncio +async def test_syncing_one_deployment_has_only_its_own_agents_follow(monkeypatch): + sent = _mock_http(monkeypatch, state={"env_get_time_url": CLOCK_URL}) + _no_servers(monkeypatch) + context = await _two_deployments(_agent("solver-a", env_step_id="deploy-a"), + _agent("solver-b", env_step_id="deploy-b")) + + await _step(env_step_id="deploy-b").execute(context) + + gateway = [r for r in sent if r.url.host == "sandbox.example"] + assert gateway and all(r.url.path.startswith("/sb-b/") for r in gateway) + assert [json.loads(r.content)["name"] for r in sent if r.url.path.endswith("/clock/followers")] == ["agent-solver-b"] + [configuration] = context.metadata["clock_configurations"] + assert configuration["deploy_step_id"] == "deploy-b" + assert [a["agent"] for a in configuration["agents"]] == ["solver-b"] + assert context.metadata[agent_clock.FOLLOWING_KEY] == { + "solver-b": {"agent": "solver-b", "env_id": "e1", "deploy_step_id": "deploy-b", "follower": "agent-solver-b"}} + + +@pytest.mark.asyncio +async def test_a_late_agent_follows_only_its_own_deployments_armed_clock(monkeypatch): + sent = _mock_http(monkeypatch) + context = await _two_deployments() + context.metadata["clock_configurations"] = [ + {"env_id": "e1", "deploy_step_id": "deploy-b", "env_get_time_url": CLOCK_URL}] + late = _agent("late", env_step_id="deploy-a") + + assert await agent_clock.follow_armed_clocks(context, late, timeout=5) == [] + assert sent == [] + + context.metadata["clock_configurations"].append({"env_id": "e1", "deploy_step_id": "deploy-a", + "env_get_time_url": CLOCK_URL}) + [outcome] = await agent_clock.follow_armed_clocks(context, late, timeout=5) + assert outcome["synced"] is True + assert str(sent[0].url) == "https://sandbox.example/sb-a/clock/followers" + + +@pytest.mark.asyncio +async def test_an_agent_bound_to_another_deployment_is_reported_and_skipped(monkeypatch): + sent = _mock_http(monkeypatch) + context = await _two_deployments() + other = context.deployed_envs[1] + + outcome = await agent_clock.follow_in(context, _agent("solver-a", env_step_id="deploy-a"), other, + env_get_time_url=CLOCK_URL, timezone=None, timeout=5) + + assert outcome == {"synced": False, "reason": "bound_to_another_deployment", "bound_to": "deploy-a"} + assert sent == [] + + +@pytest.mark.asyncio +async def test_a_teardown_retires_the_follower_at_the_deployment_it_followed(monkeypatch): + sent = _mock_http(monkeypatch, state={"env_get_time_url": CLOCK_URL}) + _no_servers(monkeypatch) + context = await _two_deployments(_agent("solver-b", env_step_id="deploy-b")) + await _step(env_step_id="deploy-b").execute(context) + del sent[:] + + assert await agent_clock.retire_followers(context, ["solver-b"]) == ["agent-solver-b"] + assert [(r.method, str(r.url)) for r in sent] == [("DELETE", "https://sandbox.example/sb-b/clock/followers")] + + +@pytest.mark.asyncio +async def test_an_agent_deployed_unbound_stays_on_the_deployment_it_got_when_another_comes(monkeypatch): + """deploy_agent without env_step_id binds to the one deployment there is; a second deployment of the env, and + arming its clock, leave the agent on the first.""" + import agent_env.a2a_agent as a2a_agent_package + from agent_env.task_step.task_steps.deploy_agent import DeployAgentTaskStep + + card = _agent().a2a_card + + class _Agent: + metadata: dict = {} + + async def deploy(self, **_): + return SimpleNamespace(a2a_url=AGENT_URL, agent_card=card, sandbox_id="sb-agent", sandbox_type="fake", + instance_id="i-1", network_policy=None, control_token=None, control_headers={}) + + monkeypatch.setattr(a2a_agent_package.A2AAgent, "get", classmethod(lambda cls, *a, **k: _Agent())) + sent = _mock_http(monkeypatch, state={"env_get_time_url": CLOCK_URL}) + _no_servers(monkeypatch) + context = await _two_deployments() + first, second = context.deployed_envs + context.deployed_envs = [first] + + await DeployAgentTaskStep(id="deploy", version=None, a2a_agent_id="a", agent_name="solver", + env_ids=["e1"]).execute(context) + [agent] = context.deployed_agents + assert agent.env_bindings == {"e1": "deploy-a"} + assert DeployedAgent.from_dict(dataclasses.asdict(agent)).env_bindings == {"e1": "deploy-a"} + context.deployed_envs.append(second) + del sent[:] + + await _step(env_step_id="deploy-b").execute(context) + + assert not any(r.url.path.endswith("/clock/followers") or r.url.host == "solver" for r in sent) + assert context.metadata["clock_configurations"][-1]["agents"] == [] + assert agent_clock.FOLLOWING_KEY not in context.metadata + + +def test_an_agent_record_from_before_bindings_loads_bound_to_any_deployment(): + agent = DeployedAgent.from_dict({"agent_name": "solver", "api_url": AGENT_URL, "env_ids": ["e1"]}) + assert agent.env_bindings == {} diff --git a/tst/unit/task_step/test_env_setup_steps_env_binding.py b/tst/unit/task_step/test_env_setup_steps_env_binding.py new file mode 100644 index 000000000..fc4d4921d --- /dev/null +++ b/tst/unit/task_step/test_env_setup_steps_env_binding.py @@ -0,0 +1,128 @@ +"""sync_env_clock, modify_env_tool_access and register_env_triggers act on the deployment their +env_step_id names when one task deploys the same env more than once. Only the +gateway HTTP boundary is mocked.""" + +from __future__ import annotations + +import httpx +import pytest + +from agent_env.env.env import DeployedEnv, DeployedGatewayEnv +from agent_env.env.gateway.constants import GATEWAY_EXTENSIONS, WELL_KNOWN_PATH +from agent_env.task_step.context import TaskStepContext +from agent_env.task_step.task_step import TaskStep +from agent_env.task_step.task_steps.modify_env_tool_access import ModifyEnvToolAccessStep +from agent_env.task_step.task_steps.register_env_triggers import RegisterEnvTriggersStep +from agent_env.task_step.task_steps.sync_env_clock import SyncEnvClockTaskStep + +ENV_ID = "env-x" + + +def _sync_clock(env_step_id): + return SyncEnvClockTaskStep(id="clock", version=None, env_id=ENV_ID, virtual_time="2025-03-14T09:00:00Z", + env_step_id=env_step_id) + + +def _tool_access(env_step_id): + return ModifyEnvToolAccessStep(id="tools", version=None, env_id=ENV_ID, action="disable", role="*", + tools=["world_tool"], env_step_id=env_step_id) + + +def _triggers(env_step_id): + return RegisterEnvTriggersStep(id="triggers", version=None, env_id=ENV_ID, triggers=[{"id": "t1"}], + env_step_id=env_step_id) + + +STEPS = pytest.mark.parametrize("make_step", [_sync_clock, _tool_access, _triggers], + ids=["sync_env_clock", "modify_env_tool_access", "register_env_triggers"]) + + +@STEPS +def test_env_step_id_round_trips(make_step): + step = make_step("deploy-env-b") + data = step.to_dict() + assert data["env_step_id"] == "deploy-env-b" + assert type(step).from_dict(data).env_step_id == "deploy-env-b" + assert type(step).from_dict({k: v for k, v in data.items() if k != "env_step_id"}).env_step_id is None + + +@pytest.mark.asyncio +@STEPS +async def test_each_step_targets_the_deployment_its_env_step_id_names(monkeypatch, make_step): + sent = _mock_gateways(monkeypatch) + context = TaskStepContext(deployed_envs=[_lane("a"), _lane("b")], metadata={}) + + await _run(monkeypatch, make_step("deploy-env-b"), context) + + assert sent and {r.url.host for r in sent} == {"gw-b"} + + +@pytest.mark.asyncio +@STEPS +async def test_env_step_id_naming_no_deployment_raises_before_any_request(monkeypatch, make_step): + sent = _mock_gateways(monkeypatch) + context = TaskStepContext(deployed_envs=[_lane("a"), _lane("b")], metadata={}) + + with pytest.raises(RuntimeError, match="env_step_id 'deploy-env-typo' names no deployment"): + await _run(monkeypatch, make_step("deploy-env-typo"), context) + assert sent == [] + + +@pytest.mark.asyncio +@STEPS +async def test_several_deployments_without_env_step_id_raise(monkeypatch, make_step): + sent = _mock_gateways(monkeypatch) + context = TaskStepContext(deployed_envs=[_lane("a"), _lane("b")], metadata={}) + + with pytest.raises(RuntimeError, match="2 deployments of env 'env-x'"): + await _run(monkeypatch, make_step(None), context) + assert sent == [] + + +@pytest.mark.asyncio +@STEPS +async def test_a_single_deployment_resolves_without_env_step_id(monkeypatch, make_step): + sent = _mock_gateways(monkeypatch) + context = TaskStepContext(deployed_envs=[_lane("a")], metadata={}) + + await _run(monkeypatch, make_step(None), context) + + assert sent and {r.url.host for r in sent} == {"gw-a"} + + +@pytest.mark.asyncio +@STEPS +async def test_an_env_not_deployed_still_raises_not_found(monkeypatch, make_step): + _mock_gateways(monkeypatch) + with pytest.raises(RuntimeError, match="Env 'env-x' not found in context.deployed_envs"): + await _run(monkeypatch, make_step("deploy-env-a"), TaskStepContext(metadata={})) + + +async def _run(monkeypatch, step: TaskStep, context: TaskStepContext) -> None: + if isinstance(step, SyncEnvClockTaskStep): + monkeypatch.setattr(step, "_backing_servers", _no_backing_servers) + await step.execute(context) + + +async def _no_backing_servers(deployed) -> list: + return [] + + +def _lane(lane: str) -> DeployedEnv: + gateway = f"http://gw-{lane}:18765" + return DeployedGatewayEnv(env_id=ENV_ID, env_version=1, gateway_url=gateway, mcp_url="", db_web_url=None, + sandbox_id=f"sb-{lane}", environment_card_url=f"{gateway}{WELL_KNOWN_PATH}", + environment_card={"name": "gw", "capabilities": {"extensions": GATEWAY_EXTENSIONS}}, + metadata={"deploy_step_id": f"deploy-env-{lane}"}) + + +def _mock_gateways(monkeypatch) -> list[httpx.Request]: + """Answer every gateway request with an empty object; return the requests.""" + sent, real = [], httpx.AsyncClient + + def handle(request: httpx.Request) -> httpx.Response: + sent.append(request) + return httpx.Response(200, json={}) + + monkeypatch.setattr(httpx, "AsyncClient", lambda *a, **k: real(transport=httpx.MockTransport(handle))) + return sent diff --git a/tst/unit/task_step/test_load_artifact_env_binding.py b/tst/unit/task_step/test_load_artifact_env_binding.py index a4f377c43..60dd319f2 100644 --- a/tst/unit/task_step/test_load_artifact_env_binding.py +++ b/tst/unit/task_step/test_load_artifact_env_binding.py @@ -8,12 +8,14 @@ from __future__ import annotations import asyncio +from types import SimpleNamespace from unittest.mock import AsyncMock, MagicMock, patch import pytest -from agent_env.env.env import DeployedGatewayEnv -from agent_env.task_step.context import TaskStepContext +from agent_env.artifact import Artifact, FileArtifactUniverse +from agent_env.env.env import DeployedGatewayEnv, Env +from agent_env.task_step.context import StepContextError, TaskStepContext from agent_env.task_step.task_steps.deploy_env import DeployEnvTaskStep from agent_env.task_step.task_steps.load_artifact import LoadArtifactTaskStep @@ -40,6 +42,25 @@ def _loader(env_step_id: str | None) -> LoadArtifactTaskStep: ) +async def _load(step: LoadArtifactTaskStep, context: TaskStepContext) -> list[str]: + """Run the step with the artifact store and the env's load boundary mocked; the sandboxes it loaded into.""" + loaded_into = [] + + class _Env: + @classmethod + async def from_deployed_env(cls, deployed): + loaded_into.append(deployed.sandbox_id) + return cls() + + async def load_file_artifact_universe(self, artifact, destination_path=None): + return SimpleNamespace(destination_path="/loaded", files=[]) + + universe = MagicMock(spec=FileArtifactUniverse, id="universe", version=1, type="file_artifact_universe") + with patch.object(Artifact, "get", return_value=universe), patch.object(Env, "get", return_value=_Env()): + await step.execute(context) + return loaded_into + + async def _deploy_two_in_parallel() -> TaskStepContext: context = TaskStepContext() envs = {"env-a": _fake_env("sb-AAA"), "env-b": _fake_env("sb-BBB")} @@ -65,8 +86,8 @@ async def _deploy_one() -> TaskStepContext: async def test_each_load_targets_its_own_branch_env(): context = await _deploy_two_in_parallel() - assert _loader("env-a")._deployed_env(context).sandbox_id == "sb-AAA" - assert _loader("env-b")._deployed_env(context).sandbox_id == "sb-BBB" + assert await _load(_loader("env-a"), context) == ["sb-AAA"] + assert await _load(_loader("env-b"), context) == ["sb-BBB"] @pytest.mark.asyncio @@ -74,7 +95,7 @@ async def test_ambiguous_env_without_env_step_id_raises(): context = await _deploy_two_in_parallel() with pytest.raises(RuntimeError, match="2 deployments of env"): - _loader(None)._deployed_env(context) + await _load(_loader(None), context) @pytest.mark.asyncio @@ -82,7 +103,7 @@ async def test_stale_env_step_id_raises_even_with_one_deployment(): context = await _deploy_one() with pytest.raises(RuntimeError, match="names no deployment"): - _loader("env-typo")._deployed_env(context) + await _load(_loader("env-typo"), context) @pytest.mark.asyncio @@ -90,12 +111,13 @@ async def test_single_deployment_resolves_without_env_step_id(): """The unchanged path every existing task takes.""" context = await _deploy_one() - assert _loader(None)._deployed_env(context).sandbox_id == "sb-ONLY" + assert await _load(_loader(None), context) == ["sb-ONLY"] @pytest.mark.asyncio -async def test_env_not_deployed_resolves_to_none(): - assert _loader(None)._deployed_env(TaskStepContext()) is None +async def test_env_not_deployed_raises_not_found(): + with pytest.raises(StepContextError, match="Env 'multi-env' not found in context.deployed_envs"): + await _load(_loader(None), TaskStepContext()) @pytest.mark.asyncio