Skip to content
Merged
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
37 changes: 28 additions & 9 deletions src/agent_env/a2a_agent/clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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


Expand All @@ -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, *,
Expand All @@ -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)
Expand All @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions src/agent_env/task_step/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"),
)

Expand Down
2 changes: 2 additions & 0 deletions src/agent_env/task_step/task_steps/deploy_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 2 additions & 25 deletions src/agent_env/task_step/task_steps/load_artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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,
):
Expand All @@ -37,13 +39,15 @@ 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

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
Expand All @@ -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})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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,
):
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"),
Expand All @@ -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}
Expand Down
15 changes: 11 additions & 4 deletions src/agent_env/task_step/task_steps/sync_env_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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),
Expand All @@ -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)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
if deployed is None:
raise StepContextError(f"Env '{self.env_id}' not found in context.deployed_envs")
try:
Expand Down Expand Up @@ -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,
Expand All @@ -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"]:
Expand Down
Loading
Loading